gpt4 book ai didi

c# - ASP.NET MVC 2 中的 httppost、httpput 等属性如何工作?

转载 作者:太空狗 更新时间:2023-10-29 21:07:17 28 4
gpt4 key购买 nike

在 ASP.NET MVC 2 中,引入了几个新的操作过滤器属性,作为 ASP.NET MVC 1 中属性的“简写”;例如,应用 HttpPostAttribute 与将 [AcceptVerbs(HttpVerbs.Post)] 应用于操作方法具有相同的作用。

此外,使用更冗长的语法,可以组合不同的方法,例如,为了允许 PostDelete

现在我想知道:新属性是如何工作的?如果我同时应用 [HttpPost][HttpDelete],ASP.NET MVC 2 允许两者还是要求两者(因此什么都不允许)?

最佳答案

查看 ActionMethodSelector 的代码,似乎所有操作方法属性都必须为 IsValidForRequest 返回 true,然后该操作才会被添加到可能的匹配方法集中。由于 HttpPost 和 HttpDelete 不可能为同一个请求返回 IsValidForRequest,我希望同时使用两者将阻止该操作匹配任何请求。

这是代码中的一个有说服力的注释:

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 并显式OR 动词,如果您需要匹配其中任何一个。

编辑 -- 这里有一个 HttpPostOrDelete 属性。

[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 );
}
}

关于c# - ASP.NET MVC 2 中的 httppost、httpput 等属性如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2648783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com