2

We want to route all static content (js, css, images - png, gif, jpeg, jpg) of our application to a RouteHandler. Where we'll do Best Practices for Speeding up Web Sites. Like adding ETags, Cache Control, Expires, etc for all our static content.

How can we do that?

Ramiz Uddin
  • 4,249
  • 4
  • 40
  • 72
  • I'm pretty sure most of that can be done with IIS ... – Martin Mar 07 '12 at 13:21
  • @Martin I would agree but there are scenarios in which you would need a handler like images need to have 'ETag' or 'If-Modified-Since' but not user profile images as they can be changed. – Ramiz Uddin Mar 07 '12 at 14:14

1 Answers1

1

You should really do it in IIS.

But if you want to have total control over it (can't find a good reason though!), you can add a catch all route as your last route. Something like:

routes.MapRoute(
    "Static",
    "{*path}",
    new { controller = "Home", action = "Static"});

Then add an action to your control to handle it:

public ActionResult Static(string path)
{
    //path is everything you get after the /
    //Use Server.MapPath to load it
    //Add headers to response, etc
    return File();
}

But this is really bad in my opinion. The most obvious thing is taking the path from the URL and map it to server. What happens if the path is /../../Windows/...? Probably nothing, but I don't like it.

Pedro
  • 1,134
  • 11
  • 26