1

I'm developing a log tha needs save all requests to a db. i created a custom filter and registered it, but it won't be calling when i request the controller.

here is the LogFilter.cs

 public class LogFilter : System.Web.Http.Filters.FilterAttribute, IFilter
{
    public void OnActionExecuting(ActionExecutedContext filterContext)
    {
        try
        {
            string entity = "";
            using (StreamReader sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
            {
                entity = sr.ReadToEnd();
            }

            logModel.RequestLog rl = new logModel.RequestLog();
            rl.IP = filterContext.HttpContext.Request.UserHostAddress;
            rl.Type = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString().ToUpper();
            rl.URL = filterContext.HttpContext.Request.Url.OriginalString;
            rl.Operation = filterContext.HttpContext.Request.HttpMethod;
            rl.RequestDate = DateTime.Now;
            if (!string.IsNullOrEmpty(entity))
                rl.Entity = entity;

            filterContext.HttpContext.Request.Cookies.Add(new HttpCookie("reqID", new deviceLog.RequestLog().Add(rl).ID.ToString()));
        }
        catch { }
    }

Global.asax

 protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);


        WebApiConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration.Filters);
    }

WebApiConfig.cs

 public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: DEFAULT_API_NAME,
            routeTemplate: DEFAULT_API_ROUTE,
            defaults: new
            {
                parameter1st = RouteParameter.Optional,
                parameter2nd = RouteParameter.Optional,
                parameter3rd = RouteParameter.Optional
            }
        );

        // remove support for xml
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        // force json indentation
        config.Formatters.JsonFormatter.Indent = true;
    }
    public static void RegisterGlobalFilters(System.Web.Http.Filters.HttpFilterCollection filters)
    {
        filters.Add(new LogFilter());
    }
Robson Gmack
  • 317
  • 2
  • 4
  • 9
  • Shouldn't the `OnActionExecuting` be an `override`? If it's not overriding a base class method then nothing will ever know to call it. – David Sep 01 '15 at 13:36

1 Answers1

2

You require HttpActionContext for Web Api under System.Web.Http.Filters namespace:

public override void OnActionExecuting(HttpActionContext actionExecutedContext)
{
    base.OnActionExecuted(actionExecutedContext);
}

Please also note override here

Neel
  • 11,625
  • 3
  • 43
  • 61
  • i changed the HttpActionExecutedContext and override the method, but it throws an error Device.WebAPI.Classes.Filters.LogFilter.OnActionExecuting(System.Web.Http.Filters.HttpActionExecutedContext)': no suitable method found to override – Robson Gmack Sep 01 '15 at 13:49
  • @RobsonGmack: Well the attribute has to override *something* in order for the framework to use it. Maybe the method is called something else? Maybe you need to implement a different base class? – David Sep 01 '15 at 14:16
  • found the problem the overide has the Parameter HttpActionContext not HttpActionExecutedContext , tnx for the help :D – Robson Gmack Sep 01 '15 at 14:57