在 ASP.NET 5 MVC 6 中提供静态文件

Serving static files in ASP.NET 5 MVC 6

我的 wwwroot 静态文件没有被解析。

我知道要提供静态文件,我需要将它们放在 wwwroot 中:

favicon.ico 解析得很好,但 schema/v1-0.json 没有。我收到通用消息:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

我在 Startup 中设置了以下内容:

app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseStaticFiles();

我正在使用 DNX beta6。以上需要 beta5 包。我在网上找不到任何有关在 beta6 中提供静态文件的信息。我不确定这是否是问题的原因。

编辑:

根据 Sirwan 的回答,我添加了以下内容,但 json 文件仍然不可用:

var options = new StaticFileOptions
{
    ContentTypeProvider =  new JsonContentTypeProvider(),
    ServeUnknownFileTypes = true,
    DefaultContentType = "application/json"
};

app.UseStaticFiles(options);

JsonContentTypeProvider class:

public class JsonContentTypeProvider : FileExtensionContentTypeProvider
{
    public JsonContentTypeProvider()
    {
        Mappings.Add(".json", "application/json");
    }
}

我什至可以在浏览服务器时看到该文件:

试试这个:

app.UseStaticFiles(new StaticFileOptions
{
    ServeUnknownFileTypes = true,
    DefaultContentType = "image/x-icon"
});

如果您有多个 ASP.NET 未知的文件类型,您可以使用 FileExtensionContentTypeProvider class:

var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".json", "application/json");
provider.Mappings.Add(".ico", "image/x-icon");
// Serve static files.
app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });

如果您使用的是 IIS,并且没有万能的托管处理程序,请确保您已添加正确的 MIME 类型映射。即使您 不需要 web.config 来让您的网站正常工作,IIS 仍会为您的网站使用它。

如果我错了,请有人纠正我,但我相信如果您没有将 IIS 配置为使用托管处理程序来提供静态文件,它仍将默认为 StaticFileModule 并调用 app.UseStaticFiles实际上并没有做任何事情。但是,如果您 运行 使用 dnx,则会使用 app.UseStaticFiles

附带说明一下,如果您还没有升级到 beta7,您可能也应该升级到 beta7。