在ASP.NET项目中在客户端处理Less文件

3

我想将文件从.css更改为.less,并使用less.js在客户端处理此.less。 因此,我将我的.css重命名为.less,然后将less.js添加到项目中(ASP.NET Web Forms)。因此,头部部分如下:

<link href="Content/myproject.less" rel="stylesheet/less" type="text/css"/> 
<script src="Scripts/less-1.5.0.min.js"></script>

但是当我运行项目时,出现了文件myproject.less未找到的错误。具体的错误信息如下:
FileError: 'http://localhost:52714/Content/myproject.less' wasn't found (404)
in myproject.less

这行代码(在myproject.less中)允许我点击myproject.less文件,打开错误页面:

HTTP Error 404.3 - Not Found

The page you are requesting cannot be served because of the extension configuration. 
If the page is a script, add a handler. If the file should be downloaded, add a 
MIME map.

Most likely causes:
•It is possible that a handler mapping is missing. By default, the static file handler
processes  all content.
•The feature you are trying to use may not be installed.
•The appropriate MIME map is not enabled for the Web site or application. (Warning: Do not 
create a MIME map for content that users should not download, such as .ASPX pages or .config
files.)
•If ASP.NET is not installed.

我的ASP.NET项目缺少什么?

1
读一下错误信息,按照其中提供的指示进行操作怎么样? - Ondrej Tucny
2个回答

5

对于那些遇到相同问题的人,我知道这是一个旧问题,但是要解决它只需将以下内容添加到您的web.config文件中(这将为LESS添加一个mime类型)。

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

在基于ASP.NET Core 2的项目中呢?这种类型的项目中没有web.config文件。 - x19

1

请确认myproject.less文件是否已设置为复制到输出目录或项目文件中的内容目录?

首先,请检查myproject.less是否实际上已经生成。如果没有,继续以下步骤。

在Visual Studio中打开项目,在myproject.less上单击左键,并检查属性窗口。有两个重要的属性需要关注:

  • Build Action - 如果设置为Content,则很可能会将该文件拷贝到构建输出中,并可供Web应用程序使用;否则不太可能。
  • Copy to Output Directory - 此选项覆盖上述Build Action行为,并确保该文件出现在构建输出中,并可供Web应用程序使用。

尝试将“复制到输出目录”设置为true,看看是否解决了问题。学习这些技术是确保Web应用程序正常运行的关键。


J. Polfer。我不知道如何检查.less文件是否设置为输出到Content目录,以及在ASP.NET项目中在哪里检查它?谢谢。 - Hidalgo
J. Polfer。非常感谢。我发现Action和Copy都没有按照您的建议设置。所以我进行了更改。很可能这会解决问题,但同时我遇到了另一个(与Ajax有关)阻止我执行的问题。但我会尽力而为。 - Hidalgo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接