ASP MVC C#:是否可以将动态值传递给属性?
发布时间:2020-05-23 20:30:05 所属栏目: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,查尔斯 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – 如何永久教Visual Studio检测我的调试信息
- asp.net-core – 有没有办法使用NuGet或其他方式打包ASP.NE
- asp.net – 映射到用户的SignalR连接
- asp.net – 通过Ajax Post – MVC3更新模型更改视图
- asp.net-mvc-3 – MVC3强制验证隐藏字段
- ASP.NET 5中的子域路由
- asp.net-mvc – 如何使用MVC控制器返回JSON
- asp.net-web-api – 如何创建角色并将用户添加到ASP.NET MV
- 从ASP.NET切换到面包和黄油HTML / jQuery的优势
- 如何创建一个asp.net会员提供者手动加密密码?
推荐文章
站长推荐
热点阅读
