how to route to css/js files in mvc.net

丶灬走出姿态 提交于 2019-11-29 14:39:47

like this

for /content/site.css

if you want to always goto 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 goto different css by providing 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
                );

I did not find a way of doing this with mvc routing what i ended up doing is: I ran this code in a 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 regex 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.

one might also have to set iis6/iis7 to route requests to static files through mvc.net but i forgot the details.

I picked this method up from several posts that i cannot remember so i apologize that i cannot give proper credit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!