ASP.NET MVC AcceptVerbs和注册路线 [英] ASP.NET MVC AcceptVerbs and registering routes

查看:253
本文介绍了ASP.NET MVC AcceptVerbs和注册路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须注册Htt​​pVerb约束在我的路由定义(当我注册路由),如果我有我的装饰动作方法的的[AcceptVerbs(..)]属性已?

do I have to register the HttpVerb constraint in my route definition (when i'm registering routes) if i have decorated my action method with the [AcceptVerbs(..)] attribute already?

如。我有这个。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formCollection)
{ .. }

我是否需要它添加到引用这一行动路线,成为制约?

do i need to add this to the route that refers to this action, as a constraint?

推荐答案

这两者之间的区别是:让我们假设创建中的问题的方法是在的HomeController

The difference between the two is the following: Let's assume the Create method in question is on the HomeController.

使用 AcceptVerbs 属性不影响路由。它实际上是由动作调用中使用的东西。什么它可以让你做的是有一个控制器2的操作方法具有相同的名称,每一个不同的HTTP方法作出回应。

Using the AcceptVerbs attribute does not affect routing. It's actually something used by the action invoker. What it allows you to do is have 2 action methods on a controller with the same name that each respond to a different HTTP Method.

public ActionResult Create(int id) { .. }

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formCollection) { .. }

所以,当 / home中的请求/创建进来,该航线将匹配和挂断请求到控制器的调用。然后调用通过查看 AcceptVerbs 属性调用正确的方法。

So when a request for /home/create comes in, the route will match and hand off the request to the controller's invoker. The invoker then invokes the correct method by looking at the AcceptVerbs attribute.

使用 HttpMethodConstraint 路由将使使得路由本身将不匹配的请求。因此,当一个POST请求进入 /家庭/创建,无论是行动的方法将被调用,因为这条路线将不匹配的请求。这有可能是另一条路线的将会的虽然匹配的要求。

Using the HttpMethodConstraint in routing will make it such that the route itself will not match the request. So when a POST request comes in for /home/create, neither action method will be called because that route will not match the request. It's possible that another route will match that request though.

对于这里的重叠部分原因是,路由是ASP.NET 3.5 SP1的一项功能,而不是具体到MVC。 MVC使用路由,但路由也使用动态数据,我们计划整合与ASP.NET Web窗体路由。

Part of the reason for the overlap here is that Routing is a feature of ASP.NET 3.5 SP1 and isn't specific to MVC. MVC uses Routing, but Routing is also used by Dynamic Data and we plan to integrate routing with ASP.NET Web Forms.

这篇关于ASP.NET MVC AcceptVerbs和注册路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆