0

I want to create the log response but, I cant get the result. I cannot hit the LogResponse file.

I have a file

public class LogResponse : ActionFilterAttribute

{
    public LogResponse()
    {

    }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Log("OnActionExecuting", filterContext.RouteData);
        Debug.WriteLine("TEST ");
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Log("OnActionExecuted", filterContext.RouteData);
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        Log("OnResultExecuting", filterContext.RouteData);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        Log("OnResultExecuted", filterContext.RouteData);
    }

    private void Log(string methodName, RouteData routeData)
    {
        var controllerName = routeData.Values["controller"];
        var actionName = routeData.Values["action"];
        var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
        System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\\test.txt");
        file.WriteLine(message);
        file.Close();
    }
}

Now, I have a home controller like :

[System.Web.Http.AllowAnonymous]
[LogResponse]
public class HomeController : ApiController
{       
    [System.Web.Http.HttpGet]

    public string Index()
    {
        return "Test API";
    }
}

Here, when I keep the debug point, I can see it hitting Index method, but it does not hit Log Response method.

I am using this for the first time. I have added a route also. And in the url, I hit it with API/Home/Index.

Can anyone let me know why i am not hitting my Log Response file ?

devam demo
  • 63
  • 1
  • 6
  • The first thing you might try is to change the class name to LogResponseAttribute. The Attribute suffix on the name is a bit of C# magic. – Prof Von Lemongargle May 03 '16 at 20:28
  • You're using [this tutorial](http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs) yes? I personally have not used ActionFiltersAttribute, but one I noticed is your method type is string as opposed to ActionResult – Arman Peiravi May 03 '16 at 20:33
  • are you sure you are inheriting from the right `ActionFilterAttribute` because depending on if you're using it on MVC or WebAPI it should inherit from a class on 2 different namespaces `System.Web.Mvc` for MVC and `System.Net.Http` for API I think. – Luiso May 03 '16 at 20:56

1 Answers1

0

You are using Web API 2. make sure your are using the right filter:

"Filters for Web API are not the same as filters for MVC. The Web API filters are found in the System.Web.Http.Filters namespace."

And to register the filter:

public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
  filters.Add(new LogRequest());
}

protected void Application_Start()
{
  RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
}
Community
  • 1
  • 1
Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38