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

前端 未结 2 1098
醉话见心
醉话见心 2020-12-21 04:33

I am trying to add an area to my application using routing in mvc.net. For controllers i added:

routes.MapRoute(
                \"Area1\", // Route name
            


        
2条回答
  •  [愿得一人]
    2020-12-21 05:12

    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.

提交回复
热议问题