Share via

MAUI Handler not working with Custom WebView and showing blank white screen.

Sushma Vaddepally (OSV) 0 Reputation points
2026-03-09T16:36:32.0833333+00:00

We are trying to call a WebView Handler and it is showing a blank white screen while using .NET MAUI8 and .NET MAUI9. We are calling below method, but it is not hitting ShouldOverrideUrlLoading() and not loading the URL properly even after passing header parameter, token correctly. This URL is working fine in Renderer.

public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)

{    

 string url = request.Url.ToString();

      if (_headerParams.Count > 0)      

  {           

  view.LoadUrl(url, _headerParams);       

  }      

  else       

  {

          view.LoadUrl(url);      

   } 

return true;  

}

When we call manually this method from OnPageStarted(), then it is going to infinite loop.

Please help me resolve this white screen issue and load URL properly.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 880 Reputation points Microsoft External Staff Moderator
    2026-03-10T09:49:58.9533333+00:00

    Hi @Sushma Vaddepally (OSV) ,

    Thanks for reaching out.

    I have tested a simple .NET MAUI 9 app that demonstrates how to properly load a URL with custom headers in a WebView on Android, without getting a blank white screen. You can refer this code example below.

    File Android/Handler/CustomWebViewHandler.cs

    
    #if ANDROID
    using Android.Webkit;
    using Microsoft.Maui.Handlers;
    using Microsoft.Maui.Platform;
    
    namespace CustomWebViewDemo;
    
    public partial class CustomWebViewHandler : WebViewHandler
    {
        protected override void ConnectHandler(Android.Webkit.WebView platformView)
        {
            base.ConnectHandler(platformView);
    
            platformView.Settings.JavaScriptEnabled = true;
            platformView.Settings.DomStorageEnabled = true;
    
            var customView = VirtualView as CustomWebView;
            var headers = customView?.Headers ?? new Dictionary<string, string>();
    
            platformView.SetWebViewClient(new CustomWebViewClient(this, headers));
        }
    }
    
    public class CustomWebViewClient : MauiWebViewClient
    {
        private readonly Dictionary<string, string> _headers;
    
        public CustomWebViewClient(WebViewHandler handler, Dictionary<string, string> headers)
            : base(handler)
        {
            _headers = headers;
        }
    
        public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
        {
            var url = request.Url?.ToString();
    
            if (_headers.Count > 0)
            {
                view.LoadUrl(url, _headers);
            }
            else
            {
                view.LoadUrl(url);
            }
    
            return true;
        }
    }
    #endif
    

    File CustomWebView.cs (in root)

    namespace CustomWebViewDemo
    {
        public class CustomWebView : WebView
        {
            public static readonly BindableProperty HeadersProperty = BindableProperty.Create(nameof(Headers), typeof(Dictionary<string, string>), typeof(CustomWebView));
    
            public Dictionary<string, string> Headers
            {
                get => (Dictionary<string, string>)GetValue(HeadersProperty);
                set => SetValue(HeadersProperty, value);
            }
        }
    }
    
    

    Hope this helps. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance provide feedback.

    Thank you.


  2. Q&A Assist
    2026-03-09T16:36:47.67+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    On Android in .NET MAUI, a blank WebView plus ShouldOverrideUrlLoading not being hit usually indicates that the native WebViewClient/handler isn’t wired the way it was in the old renderer model, or that navigation is being short‑circuited by always returning true.

    Key points and fixes:

    1. Do not always return true from ShouldOverrideUrlLoading

    Returning true tells Android: “this navigation is handled manually; do not continue with the default load.”

    In the current code:

    public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
    {
        string url = request.Url.ToString();
    
        if (_headerParams.Count > 0)
        {
            view.LoadUrl(url, _headerParams);
        }
        else
        {
            view.LoadUrl(url);
        }
    
        return true; // <- blocks default navigation
    }
    

    If this method is not called for the initial navigation, the WebView never loads anything and stays white. When it is called and you also call LoadUrl from inside it, returning true can cause re‑entry and loops.

    Typical pattern:

    • Return false when the URL should be handled by the WebView itself.
    • Return true only when handling the URL externally (e.g., opening system browser) and not calling LoadUrl again on the same WebView.

    For header injection, consider:

    public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
    {
        var url = request.Url.ToString();
    
        if (_headerParams.Count > 0)
        {
            view.LoadUrl(url, _headerParams);
            return true; // handled once, do not re‑enter
        }
    
        return false; // let WebView proceed normally when no headers
    }
    

    Then ensure the first navigation is either:

    • Started with LoadUrl(url, _headerParams) from outside ShouldOverrideUrlLoading, or
    • Allowed to proceed with return false so the WebView can render.
    1. Avoid calling ShouldOverrideUrlLoading manually

    Calling this method from OnPageStarted causes the infinite loop:

    • LoadUrl → triggers navigation → OnPageStarted → calls ShouldOverrideUrlLoadingLoadUrl again → repeat.

    ShouldOverrideUrlLoading is a callback for the WebView; it should not be invoked manually. Remove any manual calls from OnPageStarted or other events.

    1. Ensure the custom client/handler is actually attached

    In MAUI handlers, the custom Android WebViewClient must be set on the native control inside the handler’s ConnectHandler/OnConnect equivalent. If the default client is still in place, the override will never fire and the WebView stays blank.

    Verify that:

    • The custom handler is registered for WebView in MauiProgram.
    • Inside the handler, PlatformView.SetWebViewClient(new YourCustomClient(...)); (or equivalent) is called so that ShouldOverrideUrlLoading belongs to the active client.
    1. Respect MAUI/WebView lifecycle on Windows (if testing cross‑platform)

    On Windows, WebView2 is stricter about when navigation can start. If the same page is blank on Windows when created dynamically, ensure the WebView is fully attached to the visual tree before setting Source or calling LoadUrl, for example by deferring navigation until after the control is loaded.

    1. Check for _blank/multiple windows behavior

    If the target URL uses links with target="_blank", Android WebView in MAUI will not navigate unless OnCreateWindow is implemented or multiple windows are disabled. To force links to open in the same WebView, disable multiple windows in the handler:

    #if ANDROID
    Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("SupportMultipleWindows", (handler, view) =>
    {
        handler.PlatformView.Settings.SetSupportMultipleWindows(false);
    });
    #endif
    

    This prevents navigation attempts that would otherwise fail silently and appear as a white screen.

    Summary of changes to try:

    1. Remove any manual calls to ShouldOverrideUrlLoading (especially from OnPageStarted).
    2. Only return true from ShouldOverrideUrlLoading when explicitly handling the navigation and not re‑calling LoadUrl for the same request; otherwise return false.
    3. Ensure the custom WebViewClient is actually set on the native WebView in the MAUI handler.
    4. If links use target="_blank", disable multiple windows as shown above.

    References:

    0 comments No comments

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.