dotnet/wpf · error · InvalidOperationException

SR.FailedToConvertResource

Error message

SR.FailedToConvertResource

What it means

During async download handling, when the response is not a top-level container, NavigationService cannot convert the downloaded resource into a displayable object and throws. The server returned content that WPF cannot interpret as a XAML page or document for navigation.

Solutions

  1. Ensure the server returns the correct Content-Type (application/xaml+xml for XAML pages).
  2. Navigate only to XAML/resource URIs the WPF converter supports in that frame.
  3. Serve loose XAML through a properly configured web server (MIME mapping present).
  4. Handle NavigationFailed and log the destination URI to diagnose the content type.

Example fix

// server web.config: ensure MIME map
// before: <mimeMap fileExtension=".xaml" mimeType="application/octet-stream" />
// after: <mimeMap fileExtension=".xaml" mimeType="application/xaml+xml" />
Defensive patterns

Strategy: validation

Validate before calling

// client-side precheck: request headers and confirm content type before navigating
var req = WebRequest.Create(uri);
using (var resp = req.GetResponse())
    bool ok = ((HttpWebResponse)resp).ContentType.StartsWith("application/xaml+xml");

Type guard

bool IsXamlUri(Uri u) => u != null && (u.AbsolutePath.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase) || u.AbsolutePath.EndsWith(".xbap", StringComparison.OrdinalIgnoreCase));

Try / catch

try { frame.Navigate(uri); }
catch (InvalidOperationException ex) when (ex.Message.Contains("convert"))
{ frame.NavigationService.StopLoading(); ShowUnsupportedContentError(uri); }

Prevention

When it happens

Trigger: Navigating to a URI whose HTTP response content type cannot be converted to a WPF object in a non-top-level container (e.g. an inner Frame), commonly when the server sends XAML or binary content with an unexpected/unrecognized MIME type.

Common situations: Server misconfiguration returning wrong Content-Type for .xaml files; navigating a Frame to a non-XAML resource; WpfWebRequestHelper.GetContentType failing to infer XAML where IE/UrlMon would.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bc111d1e5369eb18. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs:2916

                    }
                }
                else
                {
                    try
                    {
                        // If o == null, it means we don't know how to convert it.
                        // Currently that's everything other than xaml, baml and html at site
                        // of origin. If this is not a TopLevelContainer, we will throw an exception
                        // if there is no converter for it, else we will try to launch the
                        // browser if safe to do so.
                        // For loose XAML viewing, we can get in this situation if the web server doesn't
                        // return the right MIME type. UrlMon in IE 7+ has some heuristics based on file extension
                        // to detect XAML, so PresentationHost may get invoked, but our
                        // WpfWebRequestHelper.GetContentType() fails to do the same inference. In particular,
                        // it appears that UrlMon looks at the Content-Disposition HTTP header, but we don't.
                        if (!IsTopLevelContainer)
                        {
                            throw new InvalidOperationException(SR.FailedToConvertResource);
                        }

                        DelegateToBrowser(response is PackWebResponse, destinationUri);

                        // Beware reentrancy in the context of the outgoing DelegateNavigation call:
                        // The browser will send us the BrowseStop command before returning from Navigate().
                        // This will lead to DoStopLoading(), which will abort the WebReqest.
                    }
                    finally
                    {
                        DrainResponseStreamForPartialCacheFileBug(s);

                        s.Close();

                        // Should clean the state.
                        ResetPendingNavigationState(_navStatus);
                    }
                }

View on GitHub (pinned to 81131a70a4)