asp.net-mvc ASP.NET MVC AcceptVerbs 和注册路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/283209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC AcceptVerbs and registering routes
提问by Pure.Krome
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(..)] 属性装饰了我的操作方法,我是否必须在我的路由定义中注册 HttpVerb 约束(当我注册路由时)?
eg. i have this.
例如。我有这个。
[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?
我是否需要将此添加到引用此操作的路由中,作为约束?
回答by Haacked
The difference between the two is the following: Let's assume the Createmethod in question is on the HomeController.
两者之间的区别如下:让我们假设所讨论的Create方法在HomeController.
Using the AcceptVerbsattribute 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.
使用该AcceptVerbs属性不会影响路由。它实际上是动作调用者使用的东西。它允许你做的是在一个控制器上有 2 个动作方法,每个方法都响应不同的 HTTP 方法。
public ActionResult Create(int id) { .. }
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formCollection) { .. }
So when a request for /home/createcomes 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 AcceptVerbsattribute.
因此,当请求/home/create进入时,路由将匹配并将请求移交给控制器的调用者。然后调用者通过查看AcceptVerbs属性来调用正确的方法。
Using the HttpMethodConstraintin 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 willmatch that request though.
使用HttpMethodConstraintin 路由将使路由本身与请求不匹配。因此,当 POST 请求传入 for 时/home/create,不会调用任何操作方法,因为该路由与请求不匹配。不过,另一条路线可能会匹配该请求。
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 3.5 SP1 的一项功能,并非特定于 MVC。MVC 使用路由,但动态数据也使用路由,我们计划将路由与 ASP.NET Web 窗体集成。
回答by anonymous
Nope -- Create will only respond to POST requests.
不——创建只会响应 POST 请求。
You can have other implementations of Create with different AcceptVerb attributes, or one with no attribute that will catch all other requests.
您可以拥有其他具有不同 AcceptVerb 属性的 Create 实现,或者一个没有将捕获所有其他请求的属性的实现。
If that was your only Create method, any GET (or other non-POST) request would result in a 404.
如果这是您唯一的 Create 方法,则任何 GET(或其他非 POST)请求都会导致 404。
I assume under the hood this is all being done by the routing engine anyways. [edit: nope, see Haacked's post]
我假设在幕后这一切都是由路由引擎完成的。[编辑:不,见 Haacked 的帖子]
回答by MrByte
First decorate like this:
首先装饰成这样:
[ActionName("ItemEdit"), AcceptVerbs(HttpVerbs.Post)]
public virtual object ItemSave(Menu sampleInput)
then you need to add route like this:
那么你需要像这样添加路由:
AddRoute(
"SampleEdit",
"Admin/{sampleID}/Edit",
new { controller = "Sample", action = "ItemEdit", validateAntiForgeryToken = true },
new { areaID = new IsGuid() },
new { Namespaces = controllerNamespaces }
);

