Dan*_*Dan 1 c# attributes annotations custom-attributes asp.net-mvc-3
我知道你可以像这样在C#中为方法添加一个属性,
EX1.
[HttpPost]
public void Method()
{
//code
}
Run Code Online (Sandbox Code Playgroud)
这意味着必须满足该属性才能运行Method().
我知道你可以堆叠这样的属性,
EX2.
[HttpPost]
[RequireHttps]
public void Method2()
{
//More code
}
Run Code Online (Sandbox Code Playgroud)
在您可以使用之前检查是否满足attribute1'AND'attribute2 Method2().
但你能'或'属性吗?这样的事可能吗?
EX3.
[HttpPost || RequireHttps]
public void Method3()
{
//Even more code
}
Run Code Online (Sandbox Code Playgroud)
因此,如果满足任一属性,您可以使用Method3().
编辑:对不起,印象属于名为Annotations的属性.修正了.
这意味着运行Method()必须满足注释
这是一种误解.
该[HttpPost]属性是一个指令,此方法只匹配一个Post请求.它不像安全检查那样"需求".只有一些属性以这种方式工作.
但是当把它们视为"要求"时:它们独立工作,因此总会导致AND行为.