C# 方法上的多个属性失败
我在我的方法上使用了multipleauthorizeAttribute,这给了我编译错误。这个错误只出现在构建机器上,而在我的本地机器上并没有出现 Rosyln错误CS0579:重复的“AuthorizeAttribute”属性 从下面的帖子来看,似乎可以使用多个authorize属性。我尝试在我的自定义授权属性->[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]上添加此属性,但它似乎不起作用 是否可以使用多个属性?如果是,我遗漏了什么C# 方法上的多个属性失败,c#,asp.net,C#,Asp.net,我在我的方法上使用了multipleauthorizeAttribute,这给了我编译错误。这个错误只出现在构建机器上,而在我的本地机器上并没有出现 Rosyln错误CS0579:重复的“AuthorizeAttribute”属性 从下面的帖子来看,似乎可以使用多个authorize属性。我尝试在我的自定义授权属性->[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]上添加此属性,但它似乎不起作用 是否可以使用多个属性?如果是,我
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class MyCustomAuthorizeAttribute : AuthorizeAttribute
{
}
[MyCustomAuthorizeAttribute (permissions : new[]{"Permission1", "Permission2"})]
[MyCustomAuthorizeAttribute (permissions : new[]:{"Permission3", "Permission4"})]
public string GetSomething(string someId)
{
}
避免在属性中调用任何函数。而不是
[MyCustomAuthorizeAttribute (permissions : new[]{"Permission1", "Permission2"})]
使用如下代码:
[MyCustomAuthorizeAttribute(permissions: "Permission1,Permission2")]
您可以在attributes对象中拆分字符串。在我构建的自定义授权属性中,我不添加
[AttributeUsage]
。我不知道为什么会引起问题,但试着把它拿出来。另外,正确的用法是Authorize(Roles=“role1,role2”)
。第二个属性中new[]
之后的冒号是不是应该存在的?[MyCustomAuthorizationAttribute(permissions:new[]:{“Permission3”,“Permission4”})]
属性中不能有新的运算符。它必须是可以在编译时进行评估的东西。