我知道您可以通过添加一个ActionResult方法来限制特定的AcceptVerbsAttribute方法响应哪些HTTP方法。
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index() {
...
}但是我想知道:如果没有显式的AcceptVerbs(...)属性,ActionResult方法将接受哪些HTTP方法?
我猜想是GET,HEAD和POST,但我只是想再查一遍。
谢谢。
发布于 2009-07-02 11:07:47
没有AcceptVerbsAttribute,您的Action将接受任何HTTP方法的请求。顺便说一句,您可以在RouteTable中限制HTTP方法:
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
);发布于 2009-07-02 11:08:42
它将接受所有HTTP方法。
查看来自ActionMethodSelector.cs的稍微格式化的片段(ASP.NET MVC源代码可以下载这里):
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;
}因此,如果没有更好的匹配操作方法与显式属性,将使用没有属性的动作方法。
https://stackoverflow.com/questions/1073692
复制相似问题