如何将样式应用于 ASP.NET Core Razor 视图中的标记助手?

How can style be applied to tag helpers in a ASP.NET Core Razor view?

在标准 ASP.NET 核心应用程序中,我有以下内容:

Model.cs

[Display(Name = "Foo")]
public string Foo { get;set; }

[Display(Name = "Bar")]
public string Bar{ get;set; }

Index.cshtml

    <div class="form-group">
        <div class="form-row">
            <div class="form-group col-sm-3">
                <label asp-for="Foo"></label><br />
                <label>@Model.Foo</label>
            </div>

            <div class="form-group col-sm-3">
                <label asp-for="Bar"></label><br />
                <label>@Model.Bar</label>
            </div>
        </div>
    </div>

在 site.css 内,我尝试了以下两种方法

label.bluetext{
    color: #3064b8; 
}

.bluetext{
    color: #3064b8; 
}

像这样应用样式时

<label class="bluetext">@Model.Foo</label>

但我似乎无法让它与 Razor 标签助手一起使用。

您可以按F12打开DevTools,在Sources选项卡->css文件夹->site.css.

检查自定义样式是否应用成功

如果没有,请清除缓存并重新加载页面。

尝试在参考中使用 asp-append-version="true"。它将附加一个版本作为查询字符串参数,该参数会在文件修改时发生变化。

 <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>

你也可以在Index.cshtml中直接添加样式来制作

<style>
   label.bluetext {
      color:#3064b8;
   }
</style>