I would like to achieve something very similar to this question with some improvements.
There is an ASP.NET MVC web application.
I have an entity tree.
For example, the Page class, which has the "Children" property, which is of type IList<Page> . (An instance of the Page class corresponds to a row in the database.)
Please note that site owners can add a new page at any time or delete existing ones, and URLs should also reflect these changes.
I would like to assign a unique URL for each Page in the database.
I process Page objects using a controller called PageController .
URL examples:
http://mysite.com/Page1/ http://mysite.com/Page1/SubPage/ http://mysite.com/Page/ChildPage/GrandChildPage/
You will get a picture.
Therefore, I would like each Page object to have its own URL, which is equal to its parent URL plus its own name.
In addition to this, I would also like to map one Page to the / (root) URL.
I would like to apply the following rules:
- If the URL can be processed by any other route, or if the file exists in the file system of the specified URL, suppose that the default mapping is displayed.
- If the URL can be processed by the virtual path provider, let this handle it
- If no other, map the other URLs to the
PageController class
I also found this question , and also this and this , but they did not help much, since they do not give an explanation about my first two points.
I see the following possible rules:
- Set up a route for each page. This requires me to navigate the entire tree when the application starts and add the exact matching route to the end of the route table.
- I could add a route using
{*path} and write a custom IRouteHandler that handles it, but I cannot figure out how I could handle the first two rules, since this handler could handle everything.
So far, the first solution seems to be correct, because it is also the simplest. But still, even in this case, I'm not sure how I can get PageController process requests.
I would really appreciate your thoughts on this.
Thank you in advance!
EDIT: I had time to study all aspects of every answer I received. I accepted Neil's answer as it gives a better explanation of how everything works. I also supported all the other answers as they give good ideas.
Venemo
source share