编程语言
首页 > 编程语言> > c# – 如何将动态变量传递到asp.net mvc中的authorize属性类?

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.

标签:c,asp-net,asp-net-mvc,asp-net-mvc-4,class-attributes
来源: https://codeday.me/bug/20190706/1397509.html