Routing an hierarchical path from DB with ASP.NET MVC

為{幸葍}努か 提交于 2020-01-02 04:08:05

问题


Is it possible to route an hierarchical path to map a relation from the database as follows:

Let's say I have an tuple/entity "page" with an m-t-m relation to a "page" (itself) and I want to be able to combine the slug-value of each page to find an appropriate page, like so:

mydomain.com/firstpage/secondpage/thirdpage

where firstpage, secondpage and thirdpage are of type "page" and the third page references to the second page etc.

How would you implement this with ASP.NET MVC routing?


回答1:


Ok, think I solved it!

I found out that there is a * (catch-all parameter) that can be used when routing.

For example:

routes.MapRoute(
    "Pages",
    "{*pageQuery}",
    new { controller = "Page", action = "GetPage" }
);

Then in my controller I can use regular expressions or a simple split to resolve each part of the slug. :)



来源:https://stackoverflow.com/questions/1315815/routing-an-hierarchical-path-from-db-with-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!