简体   繁体   English

Do.net 核心类目/子类目路由

[英]Dotnet core category / subcategory routing

I am creating an ASP.NET Core MVC app, in which the content comes from a database and is in a category / subcategory structure.我正在创建一个 ASP.NET Core MVC 应用程序,其中的内容来自数据库并且位于类别/子类别结构中。

What I would like to achieve within the routing, is that when a user comes in via a url like: https://mysolution.com/categoryA the category controller is called.我想在路由中实现的是,当用户通过 url 进入时,例如: https://mysolution.com/categoryA类别 controller 被调用。 When a user comes in via https://mysolution.com/categoryA/SubCategoryB the subcategory controller should be called.当用户通过https://mysolution.com/categoryA/SubCategoryB进入时,应调用子类别 controller。 So in the url I will not make use of the controller or action name, always the index action can be called and the controller based on the structure of the URL.所以在 url 中我不会使用 controller 或动作名称,总是可以调用索引动作和基于 URL 结构的 controller。

There is no need for the routing to check if the category or subcategory is existing within the database, that will be handled by the controller. The routing only has to route based on the structure of the url.路由不需要检查类别或子类别是否存在于数据库中,将由 controller 处理。路由只需根据 url 的结构进行路由。

I now have the following routing code, but not working as expected.我现在有以下路由代码,但没有按预期工作。

endpoints.MapControllerRoute(
                name: "ProductSubCategoryRoute",
                pattern: "{category}/{subcategory}",
                defaults: new { controller = "Subategory", action = "Index" }
            );

endpoints.MapControllerRoute(
                name: "ProductCategoryRoute",
                pattern: "{*category}",
                defaults: new { controller = "Category", action = "Index" }
            );

Does someone have any thoughts how to route based on the category / subcategory structure?有人对如何根据类别/子类别结构进行路由有任何想法吗?

Best regards,最好的祝福,

Jan Willem简·威廉姆

You can try to use middleware before app.UseRouting();您可以尝试在app.UseRouting(); : :

app.Use(async (context, next) =>
            {
                string pattern1 = "(category[A-Za-z]+/subcategory[A-Za-z]+)";
                string pattern2 = "(/category[A-Za-z]+)";
                var url = context.Request.Path.Value.ToLower();
                var ss = Regex.Matches(url, pattern2);
                if (Regex.Matches(url, pattern2).Count() > 0) {
                    string newurl = "";
                    if (Regex.Matches(url, pattern1).Count() > 0)
                    {
                        
                        newurl = Regex.Replace(url, pattern1, "SubCategory");
                    }
                    else {
                        newurl = Regex.Replace(url, pattern2, "Category");
                  
                    }
                    context.Response.Redirect(newurl);
                    return;
                }
               

                await next();
            });
            app.UseRouting();

And here is my endpoint:这是我的终点:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            }

The middleware can help you change https://mysolution.com/categoryA to https://mysolution.com/Category/Index ,change https://mysolution.com/categoryA/SubCategoryB to https://mysolution.com/SubCategory .中间件可以帮你把https://mysolution.com/categoryA https://mysolution.com/Category/Index ,把https://mysolution.com/categoryA/SubCategoryB https://mysolution.com/SubCategory . The sample regex can only match with Category+AZ or az,if you want to match other characters,you can change pattern.示例正则表达式只能匹配Category+AZ或az,如果你想匹配其他字符,你可以改变模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM