ASP.NET Core Razor页面多路径路由 [英] ASP.NET Core Razor Page multiple path routing

查看:460
本文介绍了ASP.NET Core Razor页面多路径路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core 2.0 Razor Pages(不是MVC)构建系统,并且无法为页面添加多个路由.例如,所有页面都应该能够通过abc.com/language/segment/shop/mypage或abc.com/language/shop/mypage到达,这两个路径均指向同一页面.段路径部分是可选的,然后页面将使用可选的段信息进行填充.

I'm building a system using ASP.NET Core 2.0 Razor Pages (not MVC) and I'm having trouble adding multiple routes for pages. For example, all pages should be able to be reached by abc.com/language/segment/shop/mypage or abc.com/language/shop/mypage, where both paths point to the same page. The segment path section is optional, then the pages do stuff with the optional segment info.

CombineTemplates标记中的问号语法似乎无效,它似乎仅在路径的最后一部分有效.浏览到{segment?}部分中没有值的URL会产生404.例如:

The question mark syntax in the CombineTemplates markup doesn't seem to work, it seems to only work in the last section of the path. Browsing to a url without a value in the {segment?} section resulted in 404. For example:

AttributeRouteModel.CombineTemplates("{language}/{segment?}/shop", selector.AttributeRouteModel.Template);

我在下面尝试过这样的代码,但它仅将两个路径相互附加,并且我需要能够将它们都设为有效.

I tried code like this below but it only appends the two paths to each other, and I need to be able to enable them both as valid.

options.Conventions.Add(new DefaultPageRouteModelConvention());
options.Conventions.Add(new SegmentPageRouteModelConvention());

在ASP.NET MVC中,我可以使用两个不同的命名MapRouteWithName添加指向同一区域/控制器/动作的两条不同的路由. 有什么想法可以使用.NET Razor Page语法做到这一点吗?

In ASP.NET MVC, I could just add two different routes pointing to the same area/controller/action with two different named MapRouteWithName. Any ideas how to do this with .NET Razor Page syntax?

推荐答案

此代码有效:

添加一个约定(不是两个不同的约定):

Add a single convention (not two different conventions):

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

在新约定中,添加两个路由选择器:

In the new convention, add both route selectors:

private class CombinedPageRouteModelConvention : IPageRouteModelConvention
    {
        private const string BaseUrlTemplateWithoutSegment = "{language}/shop";
        private const string BaseUrlTemplateWithSegment = "{language}/{segment}/shop";
        public void Apply(PageRouteModel model)
        {
            var allSelectors = new List<SelectorModel>();
            foreach (var selector in model.Selectors)
            {
                //setup the route with segment
                allSelectors.Add(CreateSelector(selector, BaseUrlTemplateWithSegment));

                //setup the route without segment
                allSelectors.Add(CreateSelector(selector, BaseUrlTemplateWithoutSegment));
            }

            //replace the default selectors with new selectors
            model.Selectors.Clear();
            foreach (var selector in allSelectors)
            {
                model.Selectors.Add(selector);
            }
        }

        private static SelectorModel CreateSelector(SelectorModel defaultSelector, string template)
        {
            var fullTemplate = AttributeRouteModel.CombineTemplates(template, defaultSelector.AttributeRouteModel.Template);
            var newSelector = new SelectorModel(defaultSelector)
            {
                AttributeRouteModel =
                {
                    Template = fullTemplate
                }
            };
            return newSelector;
        }
    }

这篇关于ASP.NET Core Razor页面多路径路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆