Lesscss and ASP.NET MVC

我们两清 提交于 2019-12-04 20:52:45

问题


Since switching my site from ASP.NET WebPages to MVC4, I'm having trouble with Lesscss.

CSS + Javascript lines:

<link href="@Url.Content("~/Content/site.css")" rel="stylesheet/less" type="text/css" />
<script src="@Url.Content("~/Scripts/less-1.3.0.min.js")" type="text/javascript"></script>

I have NO idea what is wrong. The paths are correct. The stylesheet works when you drop the '/less' from the CSS link to use it normally. When trying to use the Lesscss JS file, it loads fine BUT no sylesheet is loaded. F12 dev tools in IE also confirm that the stylesheet is not being loaded.

Has anyone else had similar issues when using Lesscss with MVC4?


回答1:


You should specify the mime type for the less file extension in your web.config and use that in your link instead if css.

<system.webServer>
  <staticContent>
    <mimeMap fileExtension=".less" mimeType="text/css" />
  </staticContent >
</system.webServer>

Personally I thing using dotless and processing this on the server is a better way to go.

Also, not really an answer to your question, but just a side point -- with MVC4 you no longer need @Url.Content(). http://www.davidhayden.me/blog/asp.net-mvc-4-the-new-tilde-slash-feature-in-razor-2




回答2:


In my opinion, a simple way to set this up better is using nuget. You don't have to use nuget but it just makes things so easy.

  1. Install dotless using nuget.
  2. Install System.Web.Optimization.Less using nuget.
  3. Add your bundle

    bundles.Add(new LessBundle("~/Content/less")
                    .Include("~/Content/less/more.less"));
    
  4. Reference the bundle

    @Styles.Render("~/Content/less")
    
  5. Done



来源:https://stackoverflow.com/questions/10779625/lesscss-and-asp-net-mvc

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