asp.netcore自定义静态文件识别

146 阅读1分钟

总所周知,wwwroot目录是项目默认存放静态文件的,我们可以通过http://xxx/xxx.html(.css|.js) 访问,但是asp.netcore识别的contentType不包含少见的文件类型或自定义文件类型,其中.less文件就访问不了

开始遇到这个问题,我也是因为需要请求静态.less样式,但发现怎么也访问不了;在微软官方文档找了半天才找到;我们只需对contentType进行扩展即可。

贴代码:

var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Add(".less", "text/css");
var fileOptions = new StaticFileOptions()
{
    ServeUnknownFileTypes = true,
    ContentTypeProvider = contentTypeProvider
};
app.UseStaticFiles(fileOptions);