简体   繁体   中英

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.

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. When a user comes in via https://mysolution.com/categoryA/SubCategoryB the subcategory controller should be called. 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.

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.

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.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 . The sample regex can only match with Category+AZ or az,if you want to match other characters,you can change pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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