ASP.NET MVC 6无法渲染CSS样式表

时间:2016-01-23 14:15:28

标签: asp.net asp.net-mvc

我正在尝试将css文件添加到我的布局文件中,该文件位于Shared文件夹中。当我运行Web应用程序时,我的CSS不会呈现(不在解决方案中的CSS(即:Bootstrap CDN)呈现。

我尝试使用这些方法链接CSS,但它们不起作用:

<link rel="stylesheet" href="@Url.Content("~/Assets/Scripts/StyleSheet.css")" type="text/css"/> 

<link rel="stylesheet" href="~/Assets/Scripts/StyleSheet.css" type="text/css"/> 

我的应用程序的文件结构是:

enter image description here

我是否应该将某些内容添加到Startup.cs文件中以便它可以读取css文件?

感谢。

编辑:错误404与样式表

我在Mozilla Firefox上运行了应用程序,并使用内置的开发人员控制台查看发生了什么。观察网络选项卡后,它会在尝试检索样式表时告诉我ERROR 404发生了。这真的很奇怪。

enter image description here

2 个答案:

答案 0 :(得分:1)

您的布局中的其他更改是否正确显示?例如,如果你添加一个随机文本字符串,它会显示出来吗?对于startup.cs,您应该在Configure部分中有app.UseStaticFiles();

再说一遍,_layout中的CSS看起来像这样:

<environment names="Development">
    <link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment names="Staging,Production">
    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

我不认为它需要被打破,但我还没试过它没有被打破......

答案 1 :(得分:0)

参考StackOverflow上发布的类似问题:ASP.NET 5 MVC6 Custom CSS & Javascript placing convention

我将CSS文件放在wwwroot目录的css文件夹中。

enter image description here

然后,在我的布局文件中,我更改了href属性:

<link rel="stylesheet" href="@Url.Content("../css/style.css")"/>

最后,我确保在配置方法中有app.UseStaticFiles():

public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();

        app.UseMvc(config => config.MapRoute(
            name: "Default",
            template: "{Controller}/{action}/{id?}",
            defaults: new {controller = "Home", action = "Index"}               
            ));
    }