加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP MVC C#:是否可以将动态值传递给属性?

发布时间:2020-12-16 00:15:11 所属栏目:asp.Net 来源:网络整理
导读:好吧,我是C#的新手,我正在尝试使用ASP MVC2创建一个小网站. 我想创建自己的授权属性.但如果可能,我需要传递一些值. 例如: [CustomAuthorize(GroupID = Method Parameter?] public ActionResult DoSomething(int GroupID) { return View(""); } 我想授权访问
好吧,我是C#的新手,我正在尝试使用ASP MVC2创建一个小网站.

我想创建自己的授权属性.但如果可能,我需要传递一些值.

例如:

[CustomAuthorize(GroupID = Method Parameter?]
    public ActionResult DoSomething(int GroupID)
    {
        return View("");
    }

我想授权访问页面.但它取决于传递给控制器??的值.因此授权取决于groupID.这有可能以任何方式实现这一目标吗?

提前致谢.

解决方法

使用值提供者:
public class CustomAuthorizeAttribute : FilterAttribute,IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var result = filterContext.Controller.ValueProvider.GetValue("GroupId"); //groupId should be of type `ValueProviderResult`

        if (result != null)
        {
            int groupId = int.Parse(result.AttemptedValue);

            //Authorize the user using the groupId   
        }
   }

}

This article可以帮到你.

HTHS,查尔斯

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读