Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我真正想要的是运行所有操作前过滤器,而不是操作本身_C#_Asp.net Mvc_Actionfilterattribute - Fatal编程技术网

C# 我真正想要的是运行所有操作前过滤器,而不是操作本身

C# 我真正想要的是运行所有操作前过滤器,而不是操作本身,c#,asp.net-mvc,actionfilterattribute,C#,Asp.net Mvc,Actionfilterattribute,在我的ASP.NETAPIWeb项目中,我使用的是ValidateModelActionHandler和AuditActionHandler。ValidateModelActionHandler调用第一个,AuditActionHandler调用第二个。如果模型验证失败,则执行流停止,第二个ActionHandler不会调用,而我必须保存一些日志信息 public class ValidateModelAttribute : ActionFilterAttribute {

在我的ASP.NETAPIWeb项目中,我使用的是ValidateModelActionHandler和AuditActionHandler。ValidateModelActionHandler调用第一个,AuditActionHandler调用第二个。如果模型验证失败,则执行流停止,第二个ActionHandler不会调用,而我必须保存一些日志信息

 public class ValidateModelAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var isValid = true;
            // dont allow null values for models for non-get operations.
            if (actionContext.Request.Method != HttpMethod.Get && actionContext.ActionArguments.Any())
            {
                var modelkv =
                    actionContext.ActionArguments.Where(p => !p.GetType().IsPrimitive && p.GetType() != typeof (string));
                var keyValuePairs = modelkv as KeyValuePair<string, object>[] ?? modelkv.ToArray();
                if (keyValuePairs.Count() == 1)
                {
                    var modelObj = keyValuePairs.First().Value;
                    if (modelObj == null)
                        isValid = false;
                }
            }

            if (!isValid || !actionContext.ModelState.IsValid)
            {
                // if query string contains debug then show detailed validation message.
                string messages = null;
                var isDebug = actionContext.Request.RequestUri.ParseQueryString()["debug"];
                if (!string.IsNullOrEmpty(isDebug))
                {
                    messages = string.Join(" ", actionContext.ModelState.Values
                                        .SelectMany(x => x.Errors)
                                        .Select(x => x.ErrorMessage));
                }

                actionContext.Response = actionContext.Request.CreateBadRequestResponse(messages);
            }
        }
    }
public类ValidateModelAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var isValid=true;
//不允许非get操作的模型为空值。
if(actionContext.Request.Method!=HttpMethod.Get&&actionContext.ActionArguments.Any())
{
无功模型kV=
actionContext.ActionArguments.Where(p=>!p.GetType().IsPrimitive&&p.GetType()!=typeof(string));
var keyValuePairs=modelkv作为KeyValuePair[]??modelkv.ToArray();
if(keyValuePairs.Count()==1)
{
var modelObj=keyValuePairs.First().Value;
if(modelObj==null)
isValid=false;
}
}
如果(!isValid | |!actionContext.ModelState.isValid)
{
//如果查询字符串包含调试,则显示详细的验证消息。
字符串消息=null;
var isDebug=actionContext.Request.RequestUri.ParseQueryString()[“debug”];
如果(!string.IsNullOrEmpty(isDebug))
{
messages=string.Join(“),actionContext.ModelState.Values
.SelectMany(x=>x.Errors)
.选择(x=>x.ErrorMessage));
}
actionContext.Response=actionContext.Request.CreateBadRequestResponse(消息);
}
}
}