Strategy for Hierarchical MVC Routing

陌路散爱 提交于 2019-12-23 05:32:31

问题


I am sure this has been answered somewhere else but I cannot seem to find a definitive posting anywhere.

Most of the postings regarding hierarchical routing talks about when you want an unlimited number of tokens in the Url. My question deals more with when it does not make sense for a given Entity to exists without being associated in the context of another Entity.

For example, I have a Contract entity along with the expected Contract controller. It has the standard Actions like Index, Edit, Create, and so on. My Urls look like

/Contracts/                   ' list all contracts
/Contracts/Create/            ' display form to create new contract
/Contracts/Edit/87Y5r3/       ' display form to edit contract 87Y5r3

Now imagine that I Order entity that must be associated with a given Contract. Using the (almost) default routing I would have Urls of

/Orders/                          ' display all orders across all contracts
/Orders/Index/87Y5r3              ' display all orders for contract 87Y5r3
/Orders/Create/87Y5r3             ' display form to create new order for contract 87Y5r3
/Orders/Edit/87Y5r3/45            ' display form to edit order 45 under contract 87Y5r3

I can of course leave the almost default routing while tweaking it to support the additional parameters like contract number and order number.

Or I can change my routing to show that Orders comes under Contracts in the hierarchy. As I see it, I have several paths to pursue:

1) Have a single controller that handles both Contracts and Orders along with numerous custom routes for mapping actions to methods. This gives me Urls along the lines of

/Contracts/                        ' maps to Index action in the Contract controller
/Contracts/Create/                 ' maps to Create action in the Contract controller
/Contracts/Orders/                 ' maps to IndexOrders action in the Contract controller
/Contracts/Orders/Index/87Y5r3     ' maps to IndexOrders action in the Contract controller
/Contracts/Orders/Edit/87Y5r3/45   ' maps to EditOrders action in the Contract controller

While I cannot imagine any good argument for having just a single controller, I am guessing that this good also be split into a Contracts controller and an Orders controller with an appropriate route(s).

The main point to take in is that the Contract Number is coming towards the end of the Url.

2) Another option is the separate controllers but with the following Urls. This seems a bit more natural (logical) to me.

/Contracts/                              ' maps to Index action in the Contract controller
/Contracts/Create/                       ' maps to Create action in the Contract controller
/Contracts/?????/87Y5r3/Orders/Index/    ' maps to Index action in the Order controller
/Contracts/?????/87Y5r3/Orders/Edit/45   ' maps to Edit action in the Order controller
/Contracts/?????/All/Orders/             ' maps to Index action in the Order controller

In this case the contract number comes after the Contract token in the Url with the order number coming towards the end. I have identified a few issues/concerns

  • How to handle Order data that goes across all Contracts. As you can see handled that with a special "All" token.

  • What action do I use for the Contracts portion of the Url. By default, routing in Mvc is /{controller}/{action}/{id}.

3) The third option I have seen posted (but do not understand enough to evaluate pros and cons) is to use RESTful API. I believe this would (could ??) resolve my second concern about what action to use for the Contract when working with Orders. The basic idea is that the action is replaced with an HTTP verb like DELETE or PUT which would only need to apply to the entity at the end of the Url.

In this case I would end up with something like

GET /Contracts/ ' maps to Index action in the Contract controller POST /Contracts/Create/ ' maps to Create action in the Contract controller GET /Contracts/87Y5r3/Orders/ ' maps to Index action in the Order controller PUT /Contracts/87Y5r3/Orders/45 ' maps to Edit action in the Order controller GET /Contracts/All/Orders/ ' maps to Index action in the Order controller

While RESTful may be the way to go I definitely do not know enough about it and my initial reaction is that it adds complexity and limitations (how many verbs are there???) that may limit its usefulness.

Based on my quick reading, going with a RESTful approach (including ASP.NET Web API as suggested by @Robotsushi below) does not really answer my question. RESTful seems to be something to be considered if my pages were requesting data via AJAX and JSON. In that sense (requesting data only) it provides an approach to Urls. However, my question is focused more on the standard MVC model where the action passes a Model to a View. At the end of the day I still need to present web pages to my users...

Did I sum this up clearly? Any other strategies that I am missing? This has to be a fairly common scenario so I am surprised I have not found a ton of articles.

I simplified the examples a bit but in my case I actually need to take this to a third level --- Contracts / Orders / Projects.

Thanks


回答1:


This is purely my opinion.

I would recommend that you use a web service. If you can't you can still use HTTP POST. It will be easier to send a complex data structure without cluttering the URL with alot of unstructured key/value pairs.

If you used this strategy you would be able to send XML or JSON as your data structure, and get the complex entity representation you probably need.

If you are unfamiliar with HTTP based web services, then check out ASP.NET Web API.

Good Luck

EDIT You can HTTP POST data to your MVC controller. If you do this then you can use a complex serialization format such as XML or JSON to send up your data. This will allow for the hierarchical nesting that your entities require.

I should have been more clear about web services. The type of operations you are performing seem like they might be better off in a web service. However regardless of whether or not you choose to use a web service your mvc controller can accept data can be correctly represented using HTTP POST actions.

I hope this helps.



来源:https://stackoverflow.com/questions/12907099/strategy-for-hierarchical-mvc-routing

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