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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 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, that is why it was written.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.