1

If I have a folder full of static content, say "/Content1/CSS", but I wish for it to be served to the client as say "/Content2/CSS" is there a way I can do this programatically where route registration typically occurs in an ASP.NET MVC application?

I understand that I can use MapRoute to map URLs to areas, controllers, and actions, and I understand I can use MapPageRoute to map URLs to individual static files. But I'm looking for a way to map a whole folder at once, without having to remap each static file so that I can for example serve all the static content under the physical path from the new virtual path.

Xefan
  • 645
  • 1
  • 12
  • 26

1 Answers1

2

By default, any URL with a file extension will bypass the MVC routing infrastructure entirely and be served directly by IIS. While you can disable this, not only is it kind of a pain, but it also slows down your application. There's a non-trivial amount of work MVC has to do to just to service a route, while letting IIS serve static files directly bypasses all that.

If you want to redirect requests from one location to another, that's a job for the URL Rewrite Module in IIS. Just create a permanent redirect from /Content1/CSS to /Content2/CSS and you're done.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I can't imagine a CSS file or any other file being different (between file types) when MVC serves them instead of directly from IIS. That being said, according to [Can asp.net-mvc return an image](http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image) the overhead is only about 1-2ms, not something to worry over unless you're website has a large number of people. However I would highly recommend using the [URL Rewrite](http://www.iis.net/downloads/microsoft/url-rewrite), that is why it was written. – Erik Philips Jul 06 '15 at 18:02
  • True, the overhead can be trivial, but I prefer to just rule the idea out completely. Unless there's actually a use case for serving via MVC (generating an image dynamically, tracking downloads, etc.), there's no point making MVC do work when IIS can handle it directly via rewrites. – Chris Pratt Jul 06 '15 at 18:06