且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC AcceptVerbs 和注册路由

更新时间:2023-02-16 16:31:03

两者的区别如下:让我们假设 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.