ASP.NET MVC dynamic routes and arbitrary depth actions

I would like to put together a forum / message board with ASP.NET MVC. The hierarchical categories of boards are quite common on these types of forums, therefore, for example:

-General discussion
-Technical support
- Website Technical Support
- Product technical support
--- Product A Technical Support
--- Product Technical Support B

Below each category there are topics and posts related to these topics. What I primarily deal with: 1.) getting to the right place, taking into account the URL, 2.) not including the boat of unnecessary information in my URL and 3.) the ability to recreate the URL from the code.

I would like the url to be something like this:

 mysite.com/Forum/ - forum index mysite.com/Forum/General-Discussion/ - board index of "general discussion" mysite.com/Forum/Technical-Support/Product/Product-A/ - board index of "Product A Tech Support "mysite.com/Forum/Technical-Support/Website/Topic1004/ - Topic index of topic with ID 1004 in the" Website Technical Support "board mysite.com/Forum/Technical-Support/Website/Topic1004/3 - Page 3 of Topic with ID 1004 

Now I have excluded Action names from this, because they can be inferred based on where I am. Each Board member in my database has an UrlPart column that is indexed, so I expect I can make relatively quick queries on this table to find out where I am.

Question: in order to find out the right place, should I use my own route handler, a custom route mediator, or should I just create obscure routing rules?

This suggestion looks pretty good, but it also looks like a lot of work for a small gain: ASP.NET MVC custom routing for searching

This seems to indicate that creating a model binding would be simpler: Dynamic MVC routes

To execute # 3, I need to create my own URL generation logic, right?

+4
source share
2 answers

If you need deep and / or inappropriate URLs, I would suggest using attribute-based routing, such as the solution discussed here .

I prefer the attribute-based approach to put each route in Application_Start because you have the best locality of the link, that is, the route specification and the controller that processes it are close to each other.

Here's how your controller actions will look for your example using the UrlRoute framework I implemented (available on codeplex ):

[UrlRoute(Path = "Forum")] public ActionResult Index() { ... } [UrlRoute(Path = "Forum/General-Discussion")] public ActionResult GeneralDiscussion() { ... } [UrlRoute(Path = "Forum/Technical-Support/Product/{productId}")] public ActionResult ProductDetails(string productId) { ... } [UrlRoute(Path = "Forum/Technical-Support/Website/{topicId}/{pageNum}")] [UrlRouteParameterDefault(Name = "pageNum", Value = "1")] public ActionResult SupportTopic(string topicId, int pageNum) { ... } 

With this approach, you can create outgoing URLs using the same helpers (Url.Route *, Url.Action *) that you would use if you manually added routes using the default route handler, without additional work.

+5
source

You can force them all to go to one controller action that handles route processing for you, manually breaking the rest of the URL and calling the method used in your BLL, which then delegates tasks to other methods, eventually returning a view () depending from your needs.

+1
source

Source: https://habr.com/ru/post/1286492/


All Articles