How to clear webview cache in .net maui?

Sowndarrajan Vijayaragavan 470 Reputation points
2023-07-27T05:19:52.8266667+00:00

Let me know how to clear the webview cache

Developer technologies | .NET | .NET MAUI
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-07-27T09:34:00.29+00:00

    Hello,

    ==================Update=============

    System.NullReferenceException: 'Object reference not set to an instance of an object.

    Please change from using Microsoft.Web.WebView2.WinForms; to using Microsoft.UI.Xaml.Controls; in the Test's background code.

    MAUI that targets Windows platform uses WinUI3, you can try getting the native platform view (WebView2) with WebViewHandler on Windows, then clean cookie by CoreWebView2CookieManager.DeleteAllCookies Method. Please refer to the following code:

    XML

    <StackLayout>  
            <Button Text="ClearCookie" Clicked="Button_Clicked"></Button>  
            <WebView x:Name="MyWebview" Source="XXXX" HeightRequest="400" WidthRequest="400" ></WebView>  
    </StackLayout>
    

    Clicking button to clean cookies.

    private void Button_Clicked(object sender, EventArgs e)
        {
    #if WINDOWS
            var platformView = MyWebview.Handler.PlatformView as WebView2;
            platformView.CoreWebView2.CookieManager.DeleteAllCookies();
    #endif
        }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.