c# – 如何将动态变量传递到asp.net mvc中的authorize属性类?

如何将动态变量传递到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();

}

我的代码发出此错误

Error 1 An attribute argument must be a constant expression, typeof
expression or array creation expression of an attribute parameter
type C:\Users\Nashat\Downloads\New folder
(3)\MvcPWy\Controllers\HomeController.cs 39 28 MvcPWy

所以请任何人帮我解决这个错误.

最佳答案 简单的答案是:你做不到.有关详细信息,请查看此 previous answer.

点赞