在 ASP.Net 中添加 .css 5 MVC 6
Adding .css in ASP.Net 5 MVC 6
我正在尝试探索 MVC6 应用程序。我选择了 Framework 4.6 和 Empty ASP.Net 预览模板。使用 Visual studio 2015.
我在 wwwroot/css
目录下有 .css 文件。
并尝试在 index.cshtml 中用作
<link href="../../css/css.css" rel="stylesheet" />
也试过
<link href="~/css/css.css" rel="stylesheet" />
但它不起作用。需要什么技巧吗?
转到您的 App_Start 文件夹 > BundleConfig.Cs > 然后找到类似于此
的代码块
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
然后在此处添加 css 文件的路径
编辑 1
现在您已告知您使用的是空预览模板,您有两个选择,
添加 App_start 文件夹并添加上面的代码块,然后在您的 Layout.cshtml 文件中添加行
@Styles.Render("~/Content/themes/base/css")
或
- 在您的 Layout.cshtml 文件引用中与您通常使用的一样,但您需要在您的共享视图中使用它,以便它贯穿网站的其余部分
你必须告诉 ASPNET5 使用静态文件。在 startup.cs 中添加
app.UseStaticFiles();
在 Configure() 函数中
试试这个
@section scripts{
<link href="~/css/css.css" rel="stylesheet" asp-file-version="true" />
}
但建议将文件添加到布局中
您需要在布局页面上添加文件,在 MVC 6 中,您可以指定要用于开发、暂存或生产环境的文件,具体取决于您的工作位置。
示例:
<environment names="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" />
<link rel="stylesheet" href="~/css/YourStyleFile.css" />
</environment>
<environment names="Staging,Production">
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>
我正在尝试探索 MVC6 应用程序。我选择了 Framework 4.6 和 Empty ASP.Net 预览模板。使用 Visual studio 2015.
我在 wwwroot/css
目录下有 .css 文件。
并尝试在 index.cshtml 中用作
<link href="../../css/css.css" rel="stylesheet" />
也试过
<link href="~/css/css.css" rel="stylesheet" />
但它不起作用。需要什么技巧吗?
转到您的 App_Start 文件夹 > BundleConfig.Cs > 然后找到类似于此
的代码块 bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
然后在此处添加 css 文件的路径
编辑 1
现在您已告知您使用的是空预览模板,您有两个选择,
添加 App_start 文件夹并添加上面的代码块,然后在您的 Layout.cshtml 文件中添加行
@Styles.Render("~/Content/themes/base/css")
或
- 在您的 Layout.cshtml 文件引用中与您通常使用的一样,但您需要在您的共享视图中使用它,以便它贯穿网站的其余部分
你必须告诉 ASPNET5 使用静态文件。在 startup.cs 中添加
app.UseStaticFiles();
在 Configure() 函数中
试试这个
@section scripts{
<link href="~/css/css.css" rel="stylesheet" asp-file-version="true" />
}
但建议将文件添加到布局中
您需要在布局页面上添加文件,在 MVC 6 中,您可以指定要用于开发、暂存或生产环境的文件,具体取决于您的工作位置。 示例:
<environment names="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" />
<link rel="stylesheet" href="~/css/YourStyleFile.css" />
</environment>
<environment names="Staging,Production">
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>