首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionResult AcceptVerbsAttribute默认的HTTP方法是什么?

ActionResult AcceptVerbsAttribute默认的HTTP方法是什么?
EN

Stack Overflow用户
提问于 2009-07-02 10:47:35
回答 2查看 2.3K关注 0票数 5

我知道您可以通过添加一个ActionResult方法来限制特定的AcceptVerbsAttribute方法响应哪些HTTP方法。

代码语言:javascript
复制
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index() {
    ...
}

但是我想知道:如果没有显式的AcceptVerbs(...)属性,ActionResult方法将接受哪些HTTP方法?

我猜想是GETHEADPOST,但我只是想再查一遍。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-07-02 11:07:47

没有AcceptVerbsAttribute,您的Action将接受任何HTTP方法的请求。顺便说一句,您可以在RouteTable中限制HTTP方法:

代码语言:javascript
复制
routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
    new { HttpMethod = new HttpMethodConstraint(
        new[] { "GET", "POST" }) }                          // Only GET or POST
);
票数 6
EN

Stack Overflow用户

发布于 2009-07-02 11:08:42

它将接受所有HTTP方法。

查看来自ActionMethodSelector.cs的稍微格式化的片段(ASP.NET MVC源代码可以下载这里):

代码语言:javascript
复制
private static List<MethodInfo> RunSelectionFilters(ControllerContext 
    controllerContext, List<MethodInfo> methodInfos) 
{
    // remove all methods which are opting out of this request
    // to opt out, at least one attribute defined on the method must 
    // return false

    List<MethodInfo> matchesWithSelectionAttributes = new List<MethodInfo>();
    List<MethodInfo> matchesWithoutSelectionAttributes = new List<MethodInfo>();

    foreach (MethodInfo methodInfo in methodInfos) 
    {
        ActionMethodSelectorAttribute[] attrs = 
            (ActionMethodSelectorAttribute[])methodInfo.
                GetCustomAttributes(typeof(ActionMethodSelectorAttribute), 
                    true /* inherit */);

        if (attrs.Length == 0) 
        {
            matchesWithoutSelectionAttributes.Add(methodInfo);
        }
        else 
            if (attrs.All(attr => attr.IsValidForRequest(controllerContext, 
                methodInfo))) 
            {
                matchesWithSelectionAttributes.Add(methodInfo);
            }
    }

    // if a matching action method had a selection attribute, 
    // consider it more specific than a matching action method
    // without a selection attribute
    return (matchesWithSelectionAttributes.Count > 0) ? 
        matchesWithSelectionAttributes : 
        matchesWithoutSelectionAttributes;
}

因此,如果没有更好的匹配操作方法与显式属性,将使用没有属性的动作方法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1073692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档