代码之家  ›  专栏  ›  技术社区  ›  mshwf

在中更改默认登录页ASP.NET核心剃须刀页面?

  •  0
  • mshwf  · 技术社区  · 7 年前

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

    引发此异常:

    AmbiguousMatchException:请求匹配多个终结点。

    页码:/Index

    页码:/NewIndex

    我发现 this

    编辑

    建议的SO线程不涉及我解释的问题,即重写默认路由而不必重命名默认路由 Index 公认的答案解决了问题。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Mike Brind    7 年前

    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

    推荐文章