微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐