2

I tried:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Index", "old");
    options.Conventions.AddPageRoute("/NewIndex", "");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

This exception is thrown:

AmbiguousMatchException: The request matched multiple endpoints. Matches:

Page: /Index

Page: /NewIndex

I found this, that suggests renaming the Index page, but it obviously, if not a good reason presented, is a workaround. Can't I just change the default page without renaming the /Index page?

EDIT

The suggested SO threads don't cover the problem I explained, which is overriding the default route without having to rename the default Index page. The accepted answer solved the problem.

mshwf
  • 7,009
  • 12
  • 59
  • 133
  • Read the answer on following link https://stackoverflow.com/questions/49035516/avoiding-request-matched-multiple-actions-resulting-in-ambiguity-error-in-asp – SUNIL DHAPPADHULE May 28 '19 at 10:30
  • Possible duplicate of [How to change starting page, using Razor Pages in .NET Core 2?](https://stackoverflow.com/questions/46117717/how-to-change-starting-page-using-razor-pages-in-net-core-2) – Brad Patton May 30 '19 at 01:10
  • Check out @sixto-saez's answer linked by Brad https://stackoverflow.com/a/55361078/5754 I checked it and it works – adinas May 30 '19 at 13:38
  • @adinas it does exactly what `AddRazorPagesOptions` do , just a shorthand version, and I'll need to change the name of the Index page – mshwf Jun 02 '19 at 14:06
  • You don't need to change the name of the index page. Just give it a different page directive. So on the first line of the Index page put `@page "/old"` for example – adinas Jun 02 '19 at 19:26

1 Answers1

3

Default pages in Razor Pages are those that have an empty string route template generated for them. You can use a custom PageRouteModelConvention to remove the empty string route template that gets generated for the Index.cshtml page, and add it instead to whichever page you want as your default page:

public class HomePageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        if(model.RelativePath == "/Pages/Index.cshtml")
        {
            var currentHomePage = model.Selectors.Single(s => s.AttributeRouteModel.Template == string.Empty);
            model.Selectors.Remove(currentHomePage);
        }

        if (model.RelativePath == "/Pages/NewIndex.cshtml")
        {
            model.Selectors.Add(new SelectorModel()
            {
                AttributeRouteModel = new AttributeRouteModel
                {
                    Template = string.Empty
                }
            });
        }
    }
}

You register the convention in ConfigureServices:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.Add(new HomePageRouteModelConvention());
}).SetCompatibilityVersion(CompatibilityVersion.Latest);

You can read more about custom page route model conventions here: https://www.learnrazorpages.com/advanced/custom-route-conventions

Mike Brind
  • 28,238
  • 6
  • 56
  • 88