Change default page in ASP.NET Core Razor Page from Index to Home

Viewed 2581

I am changing Default Page

services.AddRazorPages(options =>
{
    //...
}).AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Home", "");
});

but an exception occured

AmbiguousMatchException: The request matched multiple endpoints. Matches:
/Home
/Index

What can I do?

1 Answers

One option is to edit the @page directive in the Index.cshtml and Home.cshtml files to configure the routes:

/* Home.cshtml.cs */
@page "/"
/* Index.cshtml.cs */
@page "/Index"

This applies explicit routes for the two pages, so that the Home Razor Pages page becomes the root page, and the Index page maps only to /Index.


Note that with this approach, the call to AddPageRoute shown in your question is not needed.

Related