ASP.NET MVC URL auto-resolution in CSS files

前端 未结 6 1634
遇见更好的自我
遇见更好的自我 2021-02-02 09:43

In normal WebForms scenario, any root-relative URLs (e.g. ~/folder/file.txt) inside CSS files such as:

.form { background-image: url(~/Content/I         


        
6条回答
  •  情歌与酒
    2021-02-02 09:57

    Here are some resources on implementing IHttpModule to intercept web requests to your app...

    Write/adapt one to check for filetype (e.g. pseudocode: if (request ends with ".css") ...)

    then use a regular expression to replace all instances of "~/" with System.Web.VirtualPathUtility.ToAbsolute("~/")

    I don't know what this will do to performance, running every request through this kind of a filter, but you can probably fiddle with your web.config file and/or your MVC URL routes to funnel all .css requests through this kind of a filter while skipping past it for other files.

    Come to think of it, you can probably achieve the same effect inside an ASP.NET MVC app by pointing all your CSS refrences at a special controller.action that performs this kind of preprocessing for you. i doubt that would be as performant as an IHttpModule though.

提交回复
热议问题