如何将动态变量传递到ASP.NET MVC中的authorize属性类中?

例如,我有这段代码,如何将诸如userRoles变量之类的变量传递到Authorize属性类中?

private  string userRoles;
private string getuserRoles()
{
     //Write your code to get userRoles
     userRoles = "admin";
     return "admin";
 }

[Authorize(Roles = object.getuserRoles())]
public ActionResult Admin()
{
       ViewBag.Message = "Your contact page.";

        return View();

}

我的代码发出此错误



因此,请任何人帮我解决此错误。

最佳答案

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

您必须为Roles传递一个常量变量,如下所示:
public static class Roles
{
    public const string UserRoles = "UserRoles";
}

10-06 01:44