CSS不渲染

时间:2019-04-20 20:11:00

标签: c# html css asp.net-mvc

当我在css文件中进行更改时,运行应用程序时不会出现结果(我尝试清理/重建解决方案,但存在相同的问题)。

BundleConfig:

...
bundles.Add(new StyleBundle("~/Content/css").Include(
             "~/Content/Index.css",
             "~/Content/Layout.css",
             "~/Content/Login.css"
             ));

Index.cshtml:

@{
  ViewBag.Title = "Index";
  Layout = "../Shared/_Layout.cshtml";
 }
<head>
    <link href="@Url.Content("~/Content/Index.css")" rel="stylesheet" type="text/css" />
</head>

当我使用Chrome工具检查时:

enter image description here

在此图中,当我更改代码时,css没有更新。

1 个答案:

答案 0 :(得分:1)

如果您不希望浏览器使用CSS文件的缓存版本,则常见的解决方案是在HTML的文件URL末尾附加查询字符串。这是一种清除缓存的方法,当您对CSS或JS静态文件进行大量更改时,它对于开发/测试非常有用。

以下是实现此目的的几种快速方法:

  1. 在URL末尾手动添加查询字符串,并且每当您希望浏览器请求CSS文件的新版本时,只需更改值即可:

<link href="@(Url.Content("~/Content/Index.css") + "?v=1")" rel="stylesheet" type="text/css" />

  1. 动态添加唯一的查询字符串。如果您希望浏览器每次都请求CSS文件的新副本,这将很有用:

<link href="@(<Url.Content("~/Content/Index.css") + DateTime.Now().ToString(yyyyMMddHHmmss))" rel="stylesheet" type="text/css" />

  1. 如果您使用的是ASP.NET Core,请使用tag helper,它仅在文件发生更改时才会自动附加新的版本号。您只需添加一个简单的属性即可:

<link href="@Url.Content("~/Content/Index.css")" rel="stylesheet" type="text/css" asp-append-version="true" />