Home > Net >  Adding Blazor to existing MVC web project, "Request matched multiple endpoints" to /_Host
Adding Blazor to existing MVC web project, "Request matched multiple endpoints" to /_Host

Time:09-10

I am working on a proof of concept where I am trying to add Blazor pages and components to an existing MVC web project.

I found several tutorials online and followed them exactly. When I run the web project in Visual Studio all is well and the blazor additions work as expected. However, when the project gets deployed to our QA environment using IIS I get the following error when trying to navigate to a blazor page via website-base-address.com/test:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches: 
/_Host
/_Host

I only have one _Host.cshtml file in the project. And as I mentioned I believe I made the blazor addition correctly according to tutorials. That along with the difficulty of not being able to reproduce it locally is making me run out of things to try.

Here are what I believe to be the relevant parts of my startup.cs:

services.AddControllersWithViews().AddNewtonsoftJson(options =>
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
).AddMvcOptions(options =>
{
     options.ModelBinderProviders.Insert(0, new JsonModelBinderProvider());
}).AddRazorRuntimeCompilation()
.AddRazorOptions(opt =>
{
     opt.ViewLocationFormats.Add("/Views/Shared/AvailHeader/{0}.cshtml");
     opt.ViewLocationFormats.Add("/Views/Shared/HeaderSearch/{0}.cshtml");
     opt.ViewLocationFormats.Add("/Views/Shared/Partials/{0}.cshtml");
});

services.AddRazorPages();
services.AddServerSideBlazor();

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     else
     {
         app.UseExceptionHandler("/Error");
         app.UseHsts();
     }

     app.UseHttpsRedirection();
     app.UseStaticFiles();

     app.UseSession();
     app.UseRouting();

     app.UseAuthentication();
     app.UseAuthorization();

     app.UseEndpoints(endpoints =>
     {
          endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=Home}/{action=Index}/{id?}");

          endpoints.MapRazorPages();                
          endpoints.MapBlazorHub();
          endpoints.MapFallbackToPage("/_Host");
     });
}

Here is my _Host.cshtml:

@page "/blazor"

@namespace CampaignCreator.Web.Pages

@{
}

<app>
    @(await Html.RenderComponentAsync<App>(RenderMode.Server))
</app>

Here is my App.razor:

@using Microsoft.AspNetCore.Components.Routing

<Router AppAssembly="typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="routeData" />
    </Found>
    <NotFound>
        <h1>Page not found</h1>
        <p>Sorry, but there's nothing here!</p>
    </NotFound>
</Router>

Here is my Test.razor

@page "/test"

<h3>Test</h3>

<a href="/Home/Index">Go Home</a>

<br /><br />

<Counter />

And here is the structure of the pages and views in my project:

Controllers
    > (several controller classes)
Pages
    > Shared
      > Counter.razor  <= component used in the Test.razor page
    > _Host.cshtml
    > _Imports.razor
    > App.razor
    > Test.razor  <= This is the blazor page I am trying to navigate to
Views
    > (Several folders with views grouped by controller as usual)
    > _ViewImports.cshtml
    > _ViewStart.cshtml

CodePudding user response:

This looks like an IIS setup problem on defining the default route. My last quick check is to set @page "/blazor" to @page in _host.cshtml. It's a bit of a shot in the dark!

  • Related