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

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

问题描述

如果我已经用 [AcceptVerbs(..)] 属性装饰了我的操作方法,我是否必须在我的路由定义中注册 HttpVerb 约束(当我注册路由时)?

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?

推荐答案

两者的区别如下:让我们假设 Create 有问题的方法在 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/create 的请求传入时,路由将匹配并将请求移交给控制器的调用者.然后调用者通过查看 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 将使路由本身与请求不匹配.因此,当 /home/create 的 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天全站免登陆