asp.net-mvc-3 – 在动作过滤器中获取动作参数的值
发布时间:2020-05-23 12:24:43 所属栏目:asp.Net 来源:互联网
导读:我有一个我从jquery发布的动作: [HttpPost]public void UpdateGroupName(int groupId, string name){ authorisationRepository.UpdateGroupName(groupId, name);} 这对groupId和名称很好。我还有一些其他的群组操作,所以我想使用授权属性来确保执行更改的人
|
我有一个我从jquery发布的动作: [HttpPost]
public void UpdateGroupName(int groupId,string name)
{
authorisationRepository.UpdateGroupName(groupId,name);
}
这对groupId和名称很好。我还有一些其他的群组操作,所以我想使用授权属性来确保执行更改的人有权进行更改。 我已经有一个AuthorizationAttribute,通过访问filterContext.HttpContext.Request.Params [“groupId”]在GET请求中成功检索groupId,但是当涉及到POST时,它不起作用。 Request.Form是空的,Request.Params也是空的。 以下是我的授权属性中的代码: public int groupId { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
username = httpContext.User.Identity.Name.Split('').Last();
// awesome permissions checking goes here...
return authorized;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
groupId = int.Parse(filterContext.HttpContext.Request.Params["groupId"]); // this line throws an exception
base.OnAuthorization(filterContext);
}
我看过这个answer,但我的Form属性是空的:( 更新以显示jquery文章: var serverComm = {
post: function (url,data) {
return $.ajax({
url: url,type: 'POST',contentType: 'application/json; charset=utf-8',data: JSON.stringify(data)
});
},get: function (url,type: 'GET',cache: false,data: data
});
}
};
// some awesome code later...
serverComm.post(editGroupNameUrl,{ groupId: group.id,name: newName })
解决方法您的代码无法工作的原因是因为您将请求作为JSON字符串发送。因此,POST正文中没有请求参数,并且您无法在Request.Params中获取它们。所以,而不是: filterContext.HttpContext.Request.Params["groupId"] 使用: filterContext.Controller.ValueProvider.GetValue("groupId").AttemptedValue
这将查询值提供程序(在您的情况下,JsonValueProvider)以获取客户端发送的相应值。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – jQuery.validator.unobtrusive.adapters.
- Asp.Net GridView获取TemplateField上的当前行
- 使用实体框架在ASP.Net中创建报表
- asp.net-mvc – 在ASP.NET MVC中创建控件外的ViewResult
- asp.net – 如何创建一个.ICS文件与多个VEVENT导入到现有的
- asp.net HiddenField:动态添加自定义属性
- ASP.NET Web应用程序(.NET Framework)与ASP.NET核心Web应用
- asp.net – Microsoft Jet数据库引擎找不到对象’Sheet1 $’
- 最佳实践:ASP.NET中的CSS或主题?
- asp.net-mvc-4 – 在ASP.Net MVC 4和Autofac中注册全局过滤
推荐文章
站长推荐
- asp.net – 如何在Web / Windows窗体中将IronRub
- MVC3中的IValidatableObject – 客户端验证
- asp.net-mvc – 为什么Chrome在ASP.NET MVC中提供
- 有关ASP.NET中会话的要点
- 实体框架 – MVC 3 EF 4.1 dbContext – 删除具有
- asp.net-mvc – 如何在ASP.NET MVC中使用编译的全
- LoadControl与构造ASP.Net控件
- asp.net – 使用itemtemplate动态地将列添加到Gr
- asp.net-mvc – 从WebAPI生成MVC控制器操作的路由
- asp.net-mvc – 单元测试中的ViewResult.ViewNam
热点阅读
