CSS 文件未在 ASP.NET 5 中找到

CSS files not found in ASP.NET 5

我在 wwwroot 中创建了一个内容文件夹,因为我读到应该在文档中添加样式表和脚本等客户端内容,但是当我添加到我拥有的视图并构建项目时,它找不到文件。这样做的正确方法是什么?

<link href="~/content/style.css" rel="stylesheet" />

我很确定 wwwroot 已正确添加到 project.json。

"webroot": "wwwroot"

您需要在启动中启用 ASP.NET 核心静态文件处理:

public void Configure(IApplicationBuilder application)
{
    // Add static files to the request pipeline.
    application.UseStaticFiles();

    // Add MVC to the request pipeline.
    application.UseMvc();
}

Visual Studio 2017 (csproj)

除非您使用 Microsoft.AspNetCore.All 包,否则您还需要将其添加到您的 csproj 中:

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />

Visual Studio 2015 (xproj + project.json)

您还需要将此添加到您的 project.json:

"dependencies": {
    "Microsoft.AspNetCore.StaticFiles": "1.0.0"
    // ...Omitted
}

波浪号 ("~") 符号转换为基础 url,但这发生在服务器代码中,通过服务器控件或其他服务器端 url 操作函数。

对于客户端 URL,删除波浪号。