dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CannotNavigateToApplicationResourcesInWebBrowse…

Error message

SR.Format(SR.CannotNavigateToApplicationResourcesInWebBrowser, packUri)

What it means

ConvertPackUriToAbsoluteExternallyVisibleUri resolves a pack URI to an externally visible absolute URI for the web browser case; if a relative resource cannot be anchored to a site-of-origin and the pack URI is absolute and points at application resources, it throws InvalidOperationException because application resources cannot be navigated to in a web browser deployment. Pack application resources are internal to the app and have no external URL.

Solutions

  1. Change the resource's Build Action to 'Content' and Copy to Output Directory so it exists as a file at site-of-origin and can be addressed externally.
  2. Use a siteoforigin pack URI (pack://siteoforigin:,,,/File.xaml) for browser-navigable content.
  3. For full-trust desktop apps, navigate in-process with a NavigationWindow/Frame instead of delegating to the web browser.

Example fix

// before
// Page.xaml build action: Resource; browser navigation
webBrowser.Navigate(new Uri("pack://application:,,,/Page.xaml")); // throws
// after
// Set Page.xaml Build Action = Content, Copy to Output = Copy always
webBrowser.Navigate(new Uri("pack://siteoforigin:,,,/Page.xaml"));
Defensive patterns

Strategy: try-catch

Validate before calling

static bool CanNavigateExternally(Uri packUri) => packUri != null && packUri.IsAbsoluteUri && packUri.Scheme == "pack" && packUri.Host == "siteoforigin:,,,";

Try / catch

try { browserHost.Navigate(packUri); }
catch (InvalidOperationException ex) when (ex.Message.Contains("resource") || ex.Message.Contains("navigate")) { /* fall back to siteoforigin URI or in-app Frame */ }

Prevention

When it happens

Trigger: Navigating a Frame/WebBrowser (browser-hosted XBAP/loose XAML scenario) to a pack://application:,,, URI, or passing an absolute pack application URI into ConvertPackUriToAbsoluteExternallyVisibleUri with a relative URI that lacks a site-of-origin base.

Common situations: Trying to Navigate() to a resource compiled as Application resource (Build Action=Resource) from browser-hosted content, or pointing a hyperlink/NavigationUri at an application resource in a web deployment.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Navigation/BaseUriHelper.cs:371

                }
            }

            return sUri;
        }

        internal static Uri ConvertPackUriToAbsoluteExternallyVisibleUri(Uri packUri)
        {
            Invariant.Assert(packUri.IsAbsoluteUri && string.Equals(packUri.Scheme, PackAppBaseUri.Scheme, StringComparison.OrdinalIgnoreCase));

            Uri relative = MakeRelativeToSiteOfOriginIfPossible(packUri);

            if (! relative.IsAbsoluteUri)
            {
                return new Uri(SiteOfOriginContainer.SiteOfOrigin, relative);
            }
            else
            {
               throw new InvalidOperationException(SR.Format(SR.CannotNavigateToApplicationResourcesInWebBrowser, packUri));
            }
        }

        // If a Uri is constructed with a legacy path such as c:\foo\bar then the Uri
        // object will not correctly resolve relative Uris in some cases.  This method
        // detects and fixes this by constructing a new Uri with an original string
        // that contains the scheme file://.
        internal static Uri FixFileUri(Uri uri)
        {
            if (uri is not null && uri.IsAbsoluteUri &&
                string.Equals(uri.Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase) &&
                string.Compare(uri.OriginalString, 0, Uri.UriSchemeFile, 0, Uri.UriSchemeFile.Length, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return new Uri(uri.AbsoluteUri);
            }

            return uri;
        }

View on GitHub (pinned to 81131a70a4)