Razor页面中的默认页面是为其生成空字符串路由模板的页面。您可以使用自定义
PageRouteModelConvention
删除为
索引.cshtml
页面,并将其添加到您想要作为默认页面的任何页面:
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
}
});
}
}
}
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(new HomePageRouteModelConvention());
}).SetCompatibilityVersion(CompatibilityVersion.Latest);
https://www.learnrazorpages.com/advanced/custom-route-conventions