跳过特定操作时,过滤器过滤的行动在全球范围内注册 [英] Skip Filter on particular Action when action filter is registered globally
问题描述
i'hv写我自己的行动过滤器,并在Global.asax文件中登记的,现在我的问题是我怎么跳
该过滤器的具体行动,我想过这个通过创建例如自定义属性 DontValidate
并将其放置在为此我想跳过验证行动,并在我的行动过滤器code我把一个条件,如果动作包含 DontValidate
属性,那么跳过验证。所以目前我没有得到如何
实现它:
低于code是我的验证措施筛选
公共类ValidationActionFilter:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(HttpActionContext上下文)
{
如果(context.Request.Method.ToString()==选项)回报;
//布尔dontValidate = context.ActionDescriptor。 //这里即时通讯卡怎么办
VAR的ModelState = context.ModelState;
如果(!modelState.IsValid)
{
JsonValue误差=新的JSONObject();
的foreach(在modelState.Keys VAR键)
{
//一些东西
} context.Response = context.Request.CreateResponse< JsonValue>(的HTTPStatus code.BadRequest,错误);
}
}
}
您可以获取被用来装饰从的 ActionDescriptor
属性控制器动作的属性列表背景:
公共类ValidationActionFilter:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(HttpActionContext上下文)
{
如果(context.ActionDescriptor.GetCustomAttributes< DontValidateAttribute>()任何()。)
{
//控制器的动作与[DontValidate]装饰
//自定义属性=>没有做任何事情。
返回;
} 如果(context.Request.Method.ToString()==选项)回报;
VAR的ModelState = context.ModelState;
如果(!modelState.IsValid)
{
JsonValue误差=新的JSONObject();
的foreach(在modelState.Keys VAR键)
{
//一些东西
} context.Response = context.Request.CreateResponse< JsonValue>(的HTTPStatus code.BadRequest,错误);
}
}
}
i'hv written my own action filter and registered in global.asax file, now my problem is how do i skip
this filter for specific actions, i thought about this by creating a custom attribute for e.g DontValidate
and place it over the action for which i want to skip the validation, and in my action filter code i'll put a condition that if the action contains
DontValidate
attribute then skip the validation. So currently i'm not getting how to
implement it:
below code is my validation action filter
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.Request.Method.ToString() == "OPTIONS") return;
//bool dontValidate = context.ActionDescriptor. // here im stuck how to do
var modelState = context.ModelState;
if (!modelState.IsValid)
{
JsonValue errors = new JsonObject();
foreach (var key in modelState.Keys)
{
// some stuff
}
context.Response = context.Request.CreateResponse<JsonValue>(HttpStatusCode.BadRequest, errors);
}
}
}
You could get the list of attributes that were used to decorate the controller action from the ActionDescriptor
property of the context:
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.ActionDescriptor.GetCustomAttributes<DontValidateAttribute>().Any())
{
// The controller action is decorated with the [DontValidate]
// custom attribute => don't do anything.
return;
}
if (context.Request.Method.ToString() == "OPTIONS") return;
var modelState = context.ModelState;
if (!modelState.IsValid)
{
JsonValue errors = new JsonObject();
foreach (var key in modelState.Keys)
{
// some stuff
}
context.Response = context.Request.CreateResponse<JsonValue>(HttpStatusCode.BadRequest, errors);
}
}
}
这篇关于跳过特定操作时,过滤器过滤的行动在全球范围内注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!