title: Using ASP.NET routing to serve static files
body: <p>Can ASP.Net routing (not MVC) be used to serve static files?</p>
  
  <p>Say I want to route</p>
  
  <pre><code>http://domain.tld/static/picture.jpg
  </code></pre>
  
  <p>to</p>
  
  <pre><code>http://domain.tld/a/b/c/picture.jpg
  </code></pre>
  
  <p>and I want to do it dynamically in the sense that the rewritten URL is computed on the fly. I cannot set up a static route once and for all.</p>
  
  <p>Anyway, I can create a route like this:</p>
  
  <pre><code>routes.Add(
    "StaticRoute", new Route("static/{file}", new FileRouteHandler())
  );
  </code></pre>
  
  <p>In the <code>FileRouteHandler.ProcessRequest</code> method I can rewrite the path from <code>/static/picture.jpg</code> to <code>/a/b/c/picture.jpg</code>. I then want to create a handler for static files. ASP.NET uses the <code>StaticFileHandler</code> for this purpose. Unfortunately, this class is internal. I have tried to create the handler using reflection and it actually works:</p>
  
  <pre><code>Assembly assembly = Assembly.GetAssembly(typeof(IHttpHandler));
  Type staticFileHandlerType = assembly.GetType("System.Web.StaticFileHandler");
  ConstructorInfo constructorInfo = staticFileHandlerType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
  return (IHttpHandler) constructorInfo.Invoke(null);
  </code></pre>
  
  <p>But using internal types doesn't seem to be the proper solution. Another option is to implement my own <code>StaticFileHandler</code>, but doing so properly (supporting HTTP stuff like ranges and etags) is non-trivial.</p>
  
  <p>How should I approach routing of static files in ASP.NET?</p>