【问题标题】:ASP.NET Core 3: how to remove area from routing (from url) for one specific area?ASP.NET Core 3:如何从一个特定区域的路由(从 url)中删除区域?
【发布时间】:2020-02-14 22:49:11
【问题描述】:

如何从一个特定区域的路由(来自 url)中删除区域?

一种选择是以这种方式配置AddRazorPages(有效):

serviceCollection.AddRazorPages(
                options =>
                {
                    options.Conventions.AddAreaPageRoute("MyArea", "/MyPage1", "MyPage1");
                    options.Conventions.AddAreaPageRoute("MyArea", "/MyPage2", "MyPage2");
                    // ...                    
                });

但是我们是否可以选择在不遍历所有页面的情况下执行此操作?

类似:

serviceCollection.AddRazorPages(
                options =>
                {
                    options.Conventions.AddAreaPageRoute("MyArea", "/*", "*");

                    // ...                    
                });

【问题讨论】:

    标签: c# asp.net-core razor-pages asp.net-core-3.0


    【解决方案1】:

    您可以使用PageRouteModelConvention,这是一个实现IPageRouteModelConvention 的类,它有一个Apply 方法,您可以在其中更改路由模板(除其他外):

    public void Apply(PageRouteModel model)
    {
        if(model.RelativePath.StartsWith("/Areas/MyArea"))
        {
            foreach(var selector in model.Selectors)
            {
                selector.AttributeRouteModel.Template = selector.AttributeRouteModel.Template.Replace("MyArea", string.Empty);
            }
        }
    }
    

    ConfigureServices注册您的会议:

    options.Conventions.Add(new MyCustomPageRouteModelConvention());
    

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 2021-05-04
      • 2022-10-24
      • 1970-01-01
      • 2017-12-30
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多