2

背景

.NetCore 2 Web 应用程序以完整的 .Net 框架为目标。我们正在从传统的 MVC 分离控制器/视图结构转变,我们使用 [Route()]/Get() 属性配置路由规则来处理到 Razor 页面的路由。

至今

遵循 MS 文档(https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/razor-pages-convention-features#page-model-action-conventions)我有第一层具有类别 ID 的文件夹被传递到页面;

例如“/category/15032/dashboard/”解析为页面“ pages /category/dashboard”

`options.Conventions
        .AddFolderRouteModelConvention("/category", model =>
        {
            int selectorCount = model.Selectors.Count;
            for (int i = 0; i < selectorCount; i++)
            {
                SelectorModel selector = model.Selectors[i];
                string[] segments = selector.AttributeRouteModel.Template.Split('/');
                model.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel
                    {
                        Order = 1,
                        Template = AttributeRouteModel.CombineTemplates($"category/{{categoryid}}/{segments[1].ToLower()}", "{categoryPageTemplate?}")
                    }
                });
            }
        });`

这按预期工作。但是,当我尝试进一步增加文件夹结构的深度时,问题就出现了。

问题

每个类别都包含一个子类别,然后在其中包含一个项目。例如,我将包含一个深度嵌套的页面。

例如“/category/15032/subcategory/543/item/211/status”应该解析为页面“ pages /category/subcategory/item/status”

希望添加以下约定可以解决此问题;

`options.Conventions
        .AddFolderRouteModelConvention("/category/subcategory/item", model =>
        {
            int selectorCount = model.Selectors.Count;
            for (int i = 0; i < selectorCount; i++)
            {
                SelectorModel selector = model.Selectors[i];
                string[] segments = selector.AttributeRouteModel.Template.Split('/');
                model.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel
                    {
                        Order = 1,
                        Template = AttributeRouteModel.CombineTemplates($"category/{{categoryid}}/subcategory/{{subcategoryid}}/item/{{itemid}}/{segments[2].ToLower()}",
                                                                        "{itemPageTemplate?}")
                    }
                });
            }
        });`

错误

Microsoft.AspNetCore.Routing.RouteCreationException:属性路由信息出现以下错误:

操作:“页面:/category/subcategory/item/dashboard”错误:路由参数名称“categoryid”在路由模板中出现多次。参数名称:路由模板

在 Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetRouteInfos(IReadOnlyList 1 actions) 在 Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.AddEntries(TreeRouteBuilder builder, ActionDescriptorCollection actions) 在 Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetTreeRouter()在 Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.RouteAsync(RouteContext context) 在 Microsoft.AspNetCore.Routing.RouteCollection.d__9.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪---在 System. Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.AspNetCore.Builder.RouterMiddleware.d__4。MoveNext() --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.AspNetCore 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() .Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

问题

注意:文件夹是这样嵌套的,因为该站点有很多页面,因此保持井井有条非常重要。

我怎样才能避免这个错误?我是否必须更改 URL 结构 - 这已被证明相当易读(通常不使用 ID 号)?有没有人有任何类似嵌套结构的例子(我很难找到例子)?

4

0 回答 0