title: Wrapping StaticFileMiddleware to redirect 404 errors
body: <p>I'm trying to inject a custom middleware into my OWIN pipeline that wraps the <code>StaticFileMiddleware</code> available from MS in order to support HTML 5 mode in AngularJS.  I've been following this guide: <a href="http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx" rel="noreferrer">http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx</a></p>
  
  <p>From what I can gather of how this is supposed to work, my middleware is passing along requests to the static file middleware, and then if it can't resolve those requests (i.e., a request for an HTML 5 angular path, "/whatever"), it instead returns the base angular page so that a hard request for an HTML 5 path will work.</p>
  
  <p>My problem is that the result of invoking the inner middleware always seems to be a 200 status code, even though in my browser I get a 404, which leaves me scratching my head.  Here's my code for reference:</p>
  
  <pre><code>public static class AngularServerExtension
  {
      public static IAppBuilder UseAngularServer(this IAppBuilder builder, string rootPath, string entryPath)
      {
          var options = new AngularServerOptions()
          {
              FileServerOptions = new FileServerOptions()
              {
                  EnableDirectoryBrowsing = false,
                  FileSystem = new PhysicalFileSystem(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootPath))
              },
              EntryPath = new PathString(entryPath)
          };
  
          builder.UseDefaultFiles(options.FileServerOptions.DefaultFilesOptions);
          return builder.Use(new Func&lt;AppFunc, AppFunc&gt;(next =&gt; new AngularServerMiddleware(next, options).Invoke));           
      }
  }
  
  public class AngularServerMiddleware
  {
      private readonly AngularServerOptions _options;
      private readonly AppFunc _next;
      private readonly StaticFileMiddleware _innerMiddleware;
  
      public AngularServerMiddleware(AppFunc next, AngularServerOptions options)
      {
          _next = next;
          _options = options;
  
          _innerMiddleware = new StaticFileMiddleware(_next, options.FileServerOptions.StaticFileOptions);
      }
  
      public async Task Invoke(IDictionary&lt;string, object&gt; environment)
      {
          IOwinContext context = new OwinContext(environment);
          // try to resolve the request with default static file middleware
          await _innerMiddleware.Invoke(environment);
          Debug.WriteLine(context.Request.Path + ": " + context.Response.StatusCode);
          // *** Right here is where I would expect a 404 but I get a 200 when debugging,
          // even though my browser eventually returns a 404
  
          // route to root path if the status code is 404
          // and need support angular html5mode
          if (context.Response.StatusCode == 404 &amp;&amp; _options.Html5Mode)
          {
              context.Request.Path = _options.EntryPath;
              await _innerMiddleware.Invoke(environment);
              Console.WriteLine("&gt;&gt; " + context.Request.Path + ": " + context.Response.StatusCode);
          }
      }
  }
  public class AngularServerOptions
  {
      public FileServerOptions FileServerOptions { get; set; }
  
      public PathString EntryPath { get; set; }
  
      public bool Html5Mode
      {
          get
          {
              return EntryPath.HasValue;
          }
      }
  
      public AngularServerOptions()
      {
          FileServerOptions = new FileServerOptions();
          EntryPath = PathString.Empty;
      }
  }
  </code></pre>