如何在ASP.NET MVC 2的工作httppost,httpput等属性? [英] How do the httppost, httpput etc attributes in ASP.NET MVC 2 work?

查看:153
本文介绍了如何在ASP.NET MVC 2的工作httppost,httpput等属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC 2,一些新的动作过滤器属性的相继出台,为速记在ASP.NET MVC 1属性;例如,应用 HttpPostAttribute 做同样的事情为适用的[AcceptVerbs(HttpVerbs.Post)] 来的操作方法

In ASP.NET MVC 2, a couple of new action filter attributes were introduced, as "shorthand" for attributes in ASP.NET MVC 1; for example, applying the HttpPostAttribute does the same thing as applying [AcceptVerbs(HttpVerbs.Post)] to an action method.

此外,与更详细的语法,就可以不同的方法结合起来,以允许例如两个发表删除

In addition, with the more verbose syntax, it is possible to combine different methods, in order to allow for example both Post and Delete.

现在我想知道:你是怎么在新的属性工作?如果我申请这两个 [HttpPost] [HttpDelete] ,将ASP.NET MVC 2的允许或两者的需要的两个(从而使没有)?

Now I'm wondering: how do the new attributes work? If I apply both [HttpPost] and [HttpDelete], will ASP.NET MVC 2 allow both or require both (thus allowing nothing)?

推荐答案

纵观code代表ActionMethodSelector,似乎所有的操作方法属性必须为IsValidForRequest返回true之前的行动将被添加到组可能匹配方法。由于它是不可能的HttpPost和HttpDelete返回IsValidForRequest同一请求,我会期望同时使用将prevent从不断匹配的任何请求采取行动。

Looking at the code for ActionMethodSelector, it appears that all action method attributes must return true for IsValidForRequest before that action will be added to the set of possible matching methods. Since it's not possible for HttpPost and HttpDelete to return IsValidForRequest for the same request, I would expect that using both will prevent that action from ever matching any request.

下面是从code一个生动的注解:

Here's a telling comment from the code:

私有静态列表RunSelectionFilters(...){

  //删除这些申请
  //选择退出,在方法中定义的至少有一个属性必须返回false

private static List RunSelectionFilters(...) {
// remove all methods which are opting out of this request
// to opt out, at least one attribute defined on the method must return false

(重点煤矿)

请注意,您仍然可以使用AcceptVerbs和明确的的动词,如果你需要要匹配。

Note that you can still use AcceptVerbs and explicitly OR the verbs if you need to match either.

修改 - 这里是你的一个HttpPostOrDelete属性

EDIT -- here's an HttpPostOrDelete attribute for you.

[AttributeUsage( AttributeTargets.Method, AllowMultiple = false, Inherited = false )]
public class HttpPostOrDeleteAttribute : ActionMethodSelectorAttribute
{
    private static readonly AcceptVerbsAttribute _innerPostAttribute = new AcceptVerbsAttribute( HttpVerbs.Post );
    private static readonly AcceptVerbsAttribute _innerDeleteAttribute = new AcceptVerbsAttribute( HttpVerbs.Delete );

    public override bool IsValidForRequest( ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo )
    {
        return _innerDeleteAttribute.IsValidForRequest( controllerContext, methodInfo )
               || _innerPostAttribute.IsValidForRequest( controllerContext, methodInfo );
    }
}

这篇关于如何在ASP.NET MVC 2的工作httppost,httpput等属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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