获取一个动作过滤器中的动作参数值 [英] Getting the values of action parameters within an action filter
问题描述
我有我从jQuery的张贴到一个动作:
[HttpPost]
公共无效UpdateGroupName(INT的groupId,字符串名称)
{
authorisationRepository.UpdateGroupName(的groupId,名);
}
这正常工作与的groupId
和名称
。我有几个另一组动作,所以我想用一个授权属性,以确保执行变革的人有权做出改变。
我已经有一个 AuthorizationAttribute
检索的的groupId
成功上通过访问GET请求 filterContext.HttpContext.Request.Params [的groupId]
但是当涉及到帖子这是行不通的。在的Request.Form
是空的,所以是 Request.Params
。
这里的code我有我的授权属性:
公众诠释的groupId {搞定;组; }保护覆盖布尔AuthorizeCore(HttpContextBase的HttpContext)
{
用户名= httpContext.User.Identity.Name.Split('\\\\')最后()。 //真棒权限检查在这儿... ... 返回的授权;
}公共覆盖无效OnAuthorization(AuthorizationContext filterContext)
{
的groupId = int.Parse(filterContext.HttpContext.Request.Params [的groupId]); //这一行抛出一个异常
base.OnAuthorization(filterContext);
}
我已经看过这个回答但我的表格
属性为空:(
更新,以显示jQuery的岗位:
VAR serverComm = {
岗位:功能(URL,数据){
返回$阿贾克斯({
网址:网址,
输入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify(数据)
});
},
得到:函数(URL,数据){
返回$阿贾克斯({
网址:网址,
输入:GET,
缓存:假的,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:数据
});
}
};
//一些真棒code后...
serverComm.post(editGroupNameUrl,{的groupId:group.id,名了newName})
您code不工作的原因是因为你发送请求作为一个JSON字符串。因此,有在POST体中没有请求参数,你不能在获取它们的 Request.Params
。
而不是:
filterContext.HttpContext.Request.Params [的groupId]
使用:
filterContext.Controller.ValueProvider.GetValue(的groupId)。AttemptedValue
这将查询的价值提供者(你的情况JsonValueProvider)来获得由客户端发送相应的值。
I have an action that I am POSTing to from jquery:
[HttpPost]
public void UpdateGroupName(int groupId, string name)
{
authorisationRepository.UpdateGroupName(groupId, name);
}
This works fine with the groupId
and name
. I have a few other group actions so I would like to use an authorisation attribute to make sure the person performing the change has permission to make the change.
I already have an AuthorizationAttribute
that retrieves the groupId
successfully on GET requests by accessing filterContext.HttpContext.Request.Params["groupId"]
but when it comes to POSTs it doesn't work. The Request.Form
is empty and so is Request.Params
.
Here's the code I have in my authorisation attribute:
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);
}
I have looked at this answer but my Form
property is empty :(
Updated to show jquery post:
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, data) {
return $.ajax({
url: url,
type: 'GET',
cache: false,
contentType: 'application/json; charset=utf-8',
data: data
});
}
};
// some awesome code later...
serverComm.post(editGroupNameUrl, { groupId: group.id, name: newName })
The reason your code doesn't work is because you are sending your request as a JSON string. So there are no request parameters in the POST body and you cannot fetch them in the Request.Params
.
So instead of:
filterContext.HttpContext.Request.Params["groupId"]
use:
filterContext.Controller.ValueProvider.GetValue("groupId").AttemptedValue
This will query the value provider (in your case the JsonValueProvider) to obtain the corresponding value send by the client.
这篇关于获取一个动作过滤器中的动作参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!