ASP.NET MVC URL auto-resolution in CSS files

前端 未结 6 1637
遇见更好的自我
遇见更好的自我 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 10:17

    I created a PathHelper util class that gives me all the paths I need. For example

    " rel="Stylesheet" type="text/css"/>
    

    Gives me the correct full url with the help of System.Web.VirtualPathUtility.ToAbsolute() and my own convention (content/css/yourFile.css).

    I did the same for js, xml, t9n, pics... Its central, reusable and now I only had to change one line to catch the move of the scripts folder from content/js to Scripts in all my websites and pages.

    A moronic move if you ask me, but it's reality in the current beta :(

提交回复
热议问题