1

This question is probably a dupe, but I couldn't find a similar one (not sure exactly what to search for).

Say I have a restful resource URL like this:

/my/items/6/edit

There is a form at this page which allows me to edit my #6 item. When I submit the form, it POSTS to /my/items/6, with a PUT X-HTTP-Method-Override header.

My question is, where should the server handler get the value "6" from? Should it get it from the URL? Or from the HTTP POST data (say the id was rendered as a hidden input field on the form)?

It seems to me like it should come from the URL. However this makes it a little more trouble to get it out. For example in .NET MVC, you might get it like this from a controller action method:

var id = int.Parse(ControllerContext.RouteData.Values["id"].ToString());

...which seems like more trouble than it's worth. However, if we get it out of the HTTP POST data, then technically you could post/put data for my item #6 to /my/items/7, and the server would still save the item data under id #6.

Are there any standard practices here?

4

2 回答 2

1

检查这个: http: //www.codeproject.com/Articles/190267/Controllers-and-Routers-in-ASP-NET-MVC-3

我不是 .net 开发人员,尽管所有平台的最佳实践是将您的 URI 模板映射到控制器。路由器必须解析和准备这些信息并将其传递给您的函数/方法。

于 2012-06-02T17:52:34.600 回答
0

它绝对应该来自 URL。在 ASP.NET MVC 中,如果您有这样定义的路由:

routes.MapRoute(
    "Default",
    "/my/{controller}/{id}/{action}",
    new { controller = "items", action = "Index" }
);

您将能够从 ActionResult 方法签名中引用 ID,如下所示:

[HttpPut]
public ActionResult Index(int id, MyItemEditModel model)
{
    // id would be 6 if the URL was /my/items/6/
    // and the model need not contain the id
    ...
于 2012-06-03T04:21:17.057 回答