ASP.NET Core Exception - The request matched multiple endpoints

Raymond Raymond event 2022-08-09 visibility 6,371
more_vert

Issue context

When developing website with ASP.NET Core Razor pages, you may encounter the following exception:

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

As the exception message suggests, the error occurs when one request can be matched with more than one routes (endpoints). Thus to fix the problem, we need to find out the root cause of duplicated matching routes. 

Endpoints map

In a typical ASP.NET Core MVC application, we usually automatically map endpoint for controllers and Razor pages in a Startup class. For example, the following code snippet commonly exits in projects:

public void Configure(IApplicationBuilder app)
{
	// ...
	/*Map endpoints*/
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapRazorPages();
    });
}

It adds endpoints for controller actions and also Razor Pages.

Add page conventions

For projects using Razor Pages, another typical approach is to register page conventions when adding Razor Pages services as the following code snippet shows:

services.AddRazorPages(options =>
{
	// Customize page routes
       options.Conventions.AddAreaPageRoute("MyArea", "/Test", "/myarea/test");
})

The above code maps an area page Test.cshtml in area MyArea to endpoint "/myarea/test".

The project structure looks like the following:

ProjectFolder
      MyArea
           Pages
                Test.cshtml
                Test.cshtml.cs

Resolve the issue

For my case mentioned above, the exception occurred because I used both MapRazorPages and page conventions for the same Razor Page model. By default, the MapRazorPages function will register exactly yhe same page endpoint (/MyArea/Test) for Test page model. 

Thus to fix the problem, I just need to provide a different page convention for the Test page mode.

services.AddRazorPages(options =>
{
	// Customize page routes
       options.Conventions.AddAreaPageRoute("MyArea", "/Test", "/myarea/my-test-page");
})

In the above example, I mapped the model to route /myarea/my-test-page which is different from the default /MyArea/Test (or /myarea/test).

Drawback of this approach

The drawback of this approach is that two different URLs can reach to the same Razor Page. If you don't want this behavior, you can either remove the automatic default route mapping or remove this unnecessary convention mapping unless you have special reasons for it.

When page convention is added, the path defined in the convention will be used when constructing URLs using routing parameters.

Other reasons

There can be different other reasons for this exception. To debug, you will just need to find out whether you have accidently added duplicated routing mappings.

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts