如何从帮助程序类MVC5分配角色

时间:2015-05-01 05:25:30

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我正在做一些像行动(Get),

[Authorize(Roles = SmartRoles.smclientadmin,SmartRoles.smclientbranchadmin)]
public ActionResult Index()
{
        return View();
}

和助手班

public class SmartRoles
{
public static string smclientadmin 
{ get { return smclientadmin; } 
set { smclientadmin = "SMClientAdmin"; } }

public static string smclientbranchadmin 
{ get { return smclientbranchadmin; } 
set { smclientbranchadmin = "SMClientBranchAdmin"; } }

public static string smclientoperator 
{ get { return smclientoperator; } 
set { smclientoperator = "SMClientOperator"; } }    
}
  

错误给出的是“预期的名称属性参数”....是否有   正确的方法使用这种方法??如果是这样,请帮助...谢谢你   时间...

2 个答案:

答案 0 :(得分:1)

属性加速器将以递归调用结束。此外,属性参数必须是常量表达式。将您的代码更改为:

        [Authorize(Roles = SmartRoles.smclientadmin)]
        public ActionResult Index()
        {
            return View();
        }

        public class SmartRoles
        {
            public const string smclientadmin = "SMClientAdmin";


            public const string smclientbranchadmin = "SMClientBranchAdmin";

        }

答案 1 :(得分:1)

您创建了一个无限循环(getter正在调用自身)。更改getter以返回一个值(不需要setter)

public static string smclientadmin 
{ 
  get { return "SMClientAdmin"; } 
}