Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify ASP.NET MVC static file root

I want to be able to reorganize my ASP.NET MVC site structure to more closely match the way Rails does it (We do both rails and ASP.net at my company).

In Rails, there is a "public" folder that behaves as the root of the site. For example, I could drop in a "test.html" and access the file with the url http://domain.com/test.html which would serve up the static html file.

In asp.net MVC there is a "Content" folder that I want to behave as the root. So instead of accessing http://domain.com/content/myfile.html, i want to be able to do http://domain.com/myfile.html.

I know I can just drop the file in the root of the project, but i need to do this with many files including css, js, html, images, etc and want to share some standardized assets across rails and aspnetmvc.

Is there a way to do this?

like image 419
AlbertVo Avatar asked Jun 14 '11 18:06

AlbertVo


Video Answer


1 Answers

There is another possible solution. Instead of using code, you can use a rewrite rule to handle this for you. If you are using IIS 7 or above, you can use Microsoft's URL Rewrite Module.

A rule like the following would probably do it:

<rule name="Rewrite static files to content" stopProcessing="true">
  <match url="^([^/]+(?:\.css|\.js))$" />
  <conditions>
      <add input="{APPL_PHYSICAL_PATH}content{SCRIPT_NAME}" matchType="IsFile" />
  </conditions>
  <action type="Rewrite" url="/content/{R:1}" />
</rule>

The rule checks for a request to a css or js file off the root of the site. Then it checks to see if the file exists in the content folder. If it exists, then the rewrite will return the file in the content folder. I've only tested this a little bit, but it seems to work. It certainly needs more testing, and possible refinement.

like image 194
Nathan Fox Avatar answered Oct 12 '22 23:10

Nathan Fox