简体   繁体   English

将 ASP.NET Core Razor 页面中的默认页面从索引更改为主页

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

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?我能做什么?

One option is to edit the @page directive in the Index.cshtml and Home.cshtml files to configure the routes :一种选择是编辑Index.cshtmlHome.cshtml文件中的@page指令来配置路由

/* 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 .这为两个页面应用了显式路由,因此Home Razor Pages 页面成为根页面,而Index页面仅映射到/Index


Note that with this approach, the call to AddPageRoute shown in your question is not needed.请注意,使用这种方法, AddPageRoute在您的问题中显示对AddPageRoute的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM