title: ASP.NET webAPI event confusion on hitting a non-existing route
body: <p>I have a simple ASP.NET webAPI project with just one route. When the route is called in httpModule BeingRequest is fired first followed by EndRequest as expected.</p>
  <p>However when i hit the API with a route that doesn't exist i see the following behaivour in my HttpModule:</p>
  <p><strong>BeginRequest Called ---&gt; Begin Request Called ---&gt; EndRequest (Response code 404) ---&gt; EndRequest (Response Code 200)</strong></p>
  <p>Below is the HttpModule:</p>
  <pre><code>using System;
  using System.Web;
  public class TestModule : IHttpModule
  {
      public String ModuleName
      {
          get { return &quot;TestModule&quot;; }
      }
  
  
      public void Init(HttpApplication application)
      {
          application.BeginRequest += Application_BeginRequest;
          application.EndRequest += Application_EndRequest;
      }
  
      private void Application_BeginRequest(Object source,
          EventArgs e)
      {
          HttpApplication application = (HttpApplication)source;
          HttpContext context = application.Context;
          string filePath = context.Request.FilePath;
  
      }
  
      private void Application_EndRequest(Object source, EventArgs e)
      {
          HttpApplication application = (HttpApplication)source;
          HttpContext context = application.Context;
          string filePath = context.Request.FilePath;
      }
  
      public void Dispose() { }
  }
  </code></pre>
  <p>and my controller:</p>
  <pre><code>    public class SampleController : ApiController
      {
          [Route(&quot;Test&quot;)]
          [HttpGet]
          public IHttpActionResult WriteLog()
          {
              //omitted code
              return Ok(true);
          }
  
      }
  </code></pre>
  <p>and my standard webApiConfig:</p>
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          // Web API configuration and services
  
          // Web API routes
          config.MapHttpAttributeRoutes();
  
          config.Routes.MapHttpRoute(
              name: &quot;DefaultApi&quot;,
              routeTemplate: &quot;api/{controller}/{id}&quot;,
              defaults: new { id = RouteParameter.Optional }
          );
      }
  }
  </code></pre>
  <p>Problem is i'm logging the Response fields of each request to a database including the HttpContext.Current.Response.Status, but since the EndRequest event is raised twice i end up with 200 Status code even where the URL\route was not found.</p>
  <p>Can someone explain the issue here? is the duplicate firing of BeginRequest an expected behaviour?</p>
  <h2>UPDATE:</h2>
  <p>I've observed that this issue (BeginRequest being called twice) only occurs with attribute based routing. It does not happen when I use the conventional routing method:</p>
  <pre><code>        config.Routes.MapHttpRoute(
              name: &quot;DefaultApi&quot;,
              routeTemplate: &quot;api/{controller}&quot;
              );
  </code></pre>
  <p>I cannot disallow the usage of attribute based routing so still need to fix the issue. Any pointers?</p>