Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 控制器上的ActionFilterAttribute与action方法_C#_Asp.net_Asp.net Web Api_Action Filter_Actionfilterattribute - Fatal编程技术网

C# 控制器上的ActionFilterAttribute与action方法

C# 控制器上的ActionFilterAttribute与action方法,c#,asp.net,asp.net-web-api,action-filter,actionfilterattribute,C#,Asp.net,Asp.net Web Api,Action Filter,Actionfilterattribute,我有一个LoggingAttribute,它在OnActionExecuted方法中记录请求和响应: public class LoggingAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext httpContext) { //Logger.Log(); } } public class Valida

我有一个
LoggingAttribute
,它在
OnActionExecuted
方法中记录请求和响应:

public class LoggingAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext httpContext)
    {
            //Logger.Log();
    }
}
public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var modelState = actionContext.ModelState;
        if (!modelState.IsValid)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
        }
    }
}
还有另一个属性用于验证请求并返回
BadRequest
。此返回来自
OnActionExecuting
方法的响应:

public class LoggingAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext httpContext)
    {
            //Logger.Log();
    }
}
public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var modelState = actionContext.ModelState;
        if (!modelState.IsValid)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
        }
    }
}

现在,当我在操作方法上应用这两个属性时,不会记录我的
BadRequest
s,但当我在控制器级别应用
LoggingAttribute
和在操作方法上应用
ValidateModelAttribute
时,会记录
BadRequest
s(
LoggingAttribute
OnActionExecuted
被调用)


有人能解释一下这种行为吗,例如,
OnActionExecuted
在控制器上应用属性时,即使没有执行动作方法,也会被调用。

我尝试了如下场景

[HttpGet]
[ValidateModelAttribute]
[LoggingAttribute]
public void test()
{
   //throw new NotImplementedException("This method is not implemented");
}
使用与您相同的代码,并且我发现了与您相同的问题,您的
LogginAttribute
没有被调用,因为您正在
ValidateModelAttribute
中为上下文设置resose,当请求得到响应时,它会立即返回(因此
actionContext.response=
)当请求得到响应时,它甚至不调用您应用属性的方法

因此,这一部分的解决方案是您必须编写
OnActionExecuting
,在
Validationattribute-OnActionExecuting
方法之前调用它,并且在返回响应之前,您的代码将作为
OnActionExecuting
方法的
loggingtribute
进行日志记录

public class LoggingAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting
      (System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            //Logger.Log();
        }

   public override void OnActionExecuted(HttpActionExecutedContext httpContext)
        {
            //Logger.Log();
        }
}
同样更改顺序或属性,执行以下操作的原因是当设置了
响应时,在这种情况下,它仅从该点返回,因此不会调用管道后面的任何过滤器。

[HttpGet]
[LoggingAttribute]
[ValidateModelAttribute]
public void test()
{
   //throw new NotImplementedException("This method is not implemented");
}

正如@programtreasures在下面的回答中所建议的,您需要首先在操作方法上应用
LoggingAttribute

       [LoggingAttribute]
        [ValidateModelAttribute]        
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
[LoggingAttribute]
[验证模型属性]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}

当您在actionmethod上应用这两个属性时,您是否在LoggingAttribute上获得断点?如果在actionmethod上同时应用这两个属性-否。如果控制器级别的LoggingAttribute和action method上的ValidateModeAttribute-是。您是否尝试过在action method上更改序列?是否添加了任何全局筛选器?@programtreasures刚刚检查它是否依赖于注意顺序。如果我首先应用LoggingAttribute,它的OnActionExecuted将被调用。请在回答中添加解释。此属性不用于异常记录,它用于记录API的所有请求。此外,答案与问题无关。
LoggingAttribute的
OnActionExecuted
将被调用您提到的场景中的led。请重新检查。@PranayRana当模型无效并且在
ValidateModelAttribute
设置请求错误的响应时,它仍将调用
OnActionExecuted
LoggingAttribute
,当您在
OnActionExecuting
中设置响应时,它不会立即返回如果我错了,请点击此按钮。@C-对不起,我的错误,恢复了我的投票,您是正确的,并且+1