locked
Blazor/Negotiate 404 on Razor Pages in SubDirectories RRS feed

  • Question

  • User-939035612 posted

    Whenever I add:

    <script src="~/_framework/blazor.server.js"></script>

    to a Razor page in a subdirectory (ex: Pages/Details/Index.cshtml) the blazor.server.js file can't find blazor/negotiate because blazor.server.js is not programmed to start at the root directory when looking for the negotiate file. The 404 says that it is looking for it at /{id}/_blazor/negotiate where {id} is a route for post id, so a URL like /123/post-title routes to /Pages/Details/Index using this route

    services.AddRazorPages().AddRazorPagesOptions(options => {
                    options.Conventions.AddPageRoute("/Details/Index", "/{id:int}/{title}")

    Then blazor.server.js looks for negotiate at /123/_blazor/negotiate where of course it finds nothing.

    It looks like someone at Microsoft failed to code the blazor script properly and as a result it does not know to look for the negotiate URL in the right place. Instead of telling it to look at "~/whereverthefileiskeptontheserver.js" it just says "whereverthefileiskeptontheserver.js" which can only find the right location if the page calling blazor.server.js is in the root directory. Since blazor.server.js is in the framework I cannot edit the file directly, so how do I fix this?

    Wednesday, November 27, 2019 1:48 AM

All replies

  • User-893487980 posted

    I have had a similar issue since I started messing with Blazor in June. I wanted to add Blazor components to existing ASPNet Core MVC applications. I had to create a url rewrite...

    In the startup.cs Configure method I did the following...

    // Blazor only works from the root at present so we do url rewrites to put it in the proper location
    var rewrite = new RewriteOptions()
    .AddRewrite(@"^(.*)/_framework/blazor.boot.json", "/_framework/blazor.boot.json", skipRemainingRules: true)
    .AddRewrite(@"^(.*)/_blazor/negotiate", "/_blazor/negotiate", skipRemainingRules: true)
    .AddRewrite(@"^(.*)/_blazor?(.*)", "/_blazor?{1}", skipRemainingRules: true)
    .AddRewrite(@"^(.*)/(.*)/_framework/blazor.boot.json", "/_framework/blazor.boot.json", skipRemainingRules: true)
    .AddRewrite(@"^(.*)/(.*)/_blazor/negotiate", "/_blazor/negotiate", skipRemainingRules: true)
    .AddRewrite(@"^(.*)/(.*)/_blazor?(.*)", "/_blazor?{1}", skipRemainingRules: true);
    
    app.UseRewriter(rewrite);

    It rewrites the urls of the Blazor files to match - basically as deep as I wanted to go. I still get odd Blazor errors in the Browser's dev tools but they almost seem to be false because it has yet to actually effect the functionality of my apps. If the app is looking for www.mysite.com/Details/_blazor/negotiate, the rewrite points it to www.mysite.com/_blazor/negotiate.

    I hope this helps, I completely understand how frustrating this issue is because there is simply no information on how to do this. If someone ever finds the proper way of doing this, please let me know.

    Tuesday, January 7, 2020 7:35 PM
  • User1189080212 posted

    The answer is here:

    https://docs.microsoft.com/en-us/aspnet/core/blazor/hosting-models?view=aspnetcore-3.1

    Add this to your Layout and you can skip the UrlRewriter.

    <base href="~/" />

    Thursday, January 30, 2020 1:36 PM