剃刀页面自定义路线

时间:2018-05-08 17:52:34

标签: asp.net-core routes asp.net-core-mvc asp.net-core-2.0 razor-pages

我正在努力获得以下路线,所以我有GlobalTemplatePageRouteModelConvention。 我用过int吗?用于区分/ 1和/ ListJson处理程序的约束但是我对它不满意,它不必要地检查每个路由的整数。 它是理想的解决方案吗?我怎么能让它变得更好?

页;

/Index.cshtml
/MainTestPage.cshtml
/TestDir/Index.cshtml
/TestDir/TestPage.cshtml

处理程序;

public void OnGet(int? id)
public void OnGetListJson(int? id)

路线;

"/",
"/1",
"/ListJson",
"/ListJson/1",

"/MainTestPage",
"/MainTestPage/1",
"/MainTestPage/ListJson",
"/MainTestPage/ListJson/1",

"/TestDir",
"/TestDir/1",
"/TestDir/ListJson",
"/TestDir/ListJson/1",

"/TestDir/TestPage",
"/TestDir/TestPage/1",
"/TestDir/TestPage/ListJson",
"/TestDir/TestPage/ListJson/1",

代码;

public class GlobalTemplatePageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        var selectorCount = model.Selectors.Count;
        var list = new List<SelectorModel>();
        var isIndexPage = model.ViewEnginePath.EndsWith("/Index", StringComparison.OrdinalIgnoreCase);
        for (var i = 0; i < selectorCount; i++)
        {
            var selector = model.Selectors[i];
            var template = selector.AttributeRouteModel.Template;
            var isIndexRoute = template.EndsWith("Index", StringComparison.OrdinalIgnoreCase);
            if (isIndexPage)
            {
                if (isIndexRoute)
                {
                    list.Add(selector);
                }
                else
                {
                    model.Selectors.Add(new SelectorModel
                    {
                        AttributeRouteModel = new AttributeRouteModel
                        {
                            Order = -2,
                            Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{id:int?}")
                        }
                    });
                    selector.AttributeRouteModel.Order = -1;
                    selector.AttributeRouteModel.Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{handler?}/{id?}");
                }
            }
            else
            {
                model.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel
                    {
                        Order = -4,
                        Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{id:int?}")
                    }
                });
                selector.AttributeRouteModel.Order = -3;
                selector.AttributeRouteModel.Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{handler?}/{id?}");
            }
        }
        foreach (var selector in list)
            model.Selectors.Remove(selector);
    }
}

builder.AddRazorPagesOptions(o => {
    o.Conventions.Add(new GlobalTemplatePageRouteModelConvention());
} 

1 个答案:

答案 0 :(得分:0)

最后,我更喜欢遵循路由模板约定,因此消除了int约束,并且因为它们而不会丢失性能。

  • 模板是全局指定的,未在每页的@page指令中指定。
  • 索引路由(/ Index / 1)适用于未命名的处理程序,OnGet等。
  • 适用于以下路线;
"/",
"/Index/1",
"/ListJson",
"/ListJson/1",

"/MainTestPage",
"/MainTestPage/Index/1",
"/MainTestPage/ListJson",
"/MainTestPage/ListJson/1",

"/TestDir",
"/TestDir/Index/1",
"/TestDir/ListJson",
"/TestDir/ListJson/1",

"/TestDir/TestPage",
"/TestDir/TestPage/Index/1",
"/TestDir/TestPage/ListJson",
"/TestDir/TestPage/ListJson/1",
  • GlobalTemplatePageRouteModelConvention;
    • 索引路由导致不匹配,因此将其删除。
builder.AddRazorPagesOptions(o =>
{
    o.Conventions.Add(new GlobalTemplatePageRouteModelConvention());
});


public class GlobalTemplatePageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        var isIndexPage = model.ViewEnginePath.EndsWith("/Index", StringComparison.OrdinalIgnoreCase);

        foreach (var selector in model.Selectors.ToList())
        {
            var template = selector.AttributeRouteModel.Template;
            if (isIndexPage)
            {
                var isIndexRoute = template.EndsWith("Index", StringComparison.OrdinalIgnoreCase);
                if (isIndexRoute)
                {
                    model.Selectors.Remove(selector);
                    continue;
                }
            }
            selector.AttributeRouteModel.Template =
                AttributeRouteModel.CombineTemplates(template,
                    "{handler?}/{id?}");
        }
    }
}