How to redirect css / js files to mvc.net

I am trying to add scope to my application using routing in mvc.net. For controllers, I added:

routes.MapRoute( "Area1", // Route name "Area1/{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 

how can I route css / js files in the same way, i.e. I would like to have area1/content/site.css for /content/site.css or /content/area1/site.css .

thanks

+4
source share
2 answers

I did not find a way to do this with mvc routing, which I ended up with: I ran this code in the http module:

 void context_BeginRequest(object sender, EventArgs e) { HttpApplication Application = sender as HttpApplication; var match = r.Match(Application.Request.Url.AbsolutePath); if (match.Success) { var fileName = match.Groups[2].Value; Application.Context.RewritePath("/" + fileName); } } 

r is a regular expression in my case:

 private readonly Regex r = new `Regex("^/gmail(/canvas)?/((content|scripts|images|tinymce).*)$", RegexOptions.IgnoreCase);` 

in global.asax I added:

 routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|gif|jpg)(/.*)?" }); 

to prevent mvc.net from routing these requests.

it may also be necessary to install iis6 / iis7 to route requests to static files via mvc.net, but I forgot the details.

I chose this method from several posts that I don’t remember, so I apologize for not being able to give the proper credit.

+3
source

like this

for /content/site.css

if you want to always go to site.css:

 routes.MapRoute( "Area1", // Route name "/{action}/site.css", // URL with parameters new { controller = "Area1", action = "content" } // Parameter defaults ); 

and if you want to switch to another css by specifying the css name:

 routes.MapRoute( "Area1", // Route name "/{action}/{resource}.css", // URL with parameters new { controller = "Area1", action = "content", resource = UrlParameter.Optional } // Parameter defaults ); 

for /content/area1/site.css

 routes.MapRoute( "Area1", // Route name "/{action}/Area1/{resource}.css", // URL with parameters new { controller = "Area1", action = "content", resource = UrlParameter.Optional } // Parameter defaults ); 
+4
source

All Articles