本文翻译自:Styles.Render in MVC4
In a .NET MVC4
project how does @Styles.Render
works? 在.NET MVC4
项目中, @Styles.Render
如何工作?
I mean, in @Styles.Render("~/Content/css")
which file is it calling? 我的意思是,在@Styles.Render("~/Content/css")
中调用哪个文件?
I dont have a file or a folder called "css" inside my Content
folder. 我的Content
文件夹中没有文件或名为“css”的文件夹。
#1楼
参考:https://stackoom.com/question/oT8T/MVC-中的Styles-Render
#2楼
It's calling the files included in that particular bundle which is declared inside the BundleConfig
class in the App_Start
folder. 它调用包含在特定包中的文件,该包在App_Start
文件夹的BundleConfig
类中App_Start
。
In that particular case The call to @Styles.Render("~/Content/css")
is calling "~/Content/site.css". 在那种特殊情况下,对@Styles.Render("~/Content/css")
的调用是调用“〜/ Content / site.css”。
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
#3楼
Watch out for case sensitivity. 注意区分大小写。 If you have a file 如果你有一个文件
/Content/bootstrap.css /Content/bootstrap.css
and you redirect in your Bundle.config to 然后在Bundle.config中重定向到
.Include("~/Content/Bootstrap.css") .INCLUDE( “〜/内容/ Bootstrap.css”)
it will not load the css. 它不会加载CSS。
#4楼
src="@url.content("~/Folderpath/*.css")"
应该呈现样式
#5楼
As defined in App_start.BundleConfig, it's just calling 如App_start.BundleConfig中所定义,它只是调用
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
Nothing happens even if you remove that section. 即使您删除该部分也没有任何反应。
#6楼
Polo I wouldn't use Bundles in MVC for multiple reasons. Polo我不会在MVC中使用Bundles有多种原因。 It doesn't work in your case because you have to set up a custom BundleConfig class in your Apps_Start folder. 它在您的情况下不起作用,因为您必须在Apps_Start文件夹中设置自定义BundleConfig类。 This makes no sense when you can simple add a style in the head of your html like so: 当您可以在html的头部添加样式时,这没有任何意义,如下所示:
<link rel="stylesheet" href="~/Content/bootstrap.css" />
<link rel="stylesheet" href="~/Content/bootstrap.theme.css" />
You can also add these to a Layout.cshtml or partial class that's called from all your views and dropped into each page. 您还可以将这些添加到Layout.cshtml或从您的所有视图调用并放入每个页面的部分类。 If your styles change, you can easily change the name and path without having to recompile. 如果样式发生变化,您可以轻松更改名称和路径,而无需重新编译。
Adding hard-coded links to CSS in a class breaks with the whole purpose of separation of the UI and design from the application model, as well. 在类中添加CSS的硬编码链接打破了UI和设计与应用程序模型分离的全部目的。 You also don't want hard coded style sheet paths managed in c# because you can no longer build "skins" or separate style models for say different devices, themes, etc. like so: 您也不希望在c#中管理硬编码样式表路径,因为您不能再为不同的设备,主题等构建“皮肤”或单独的样式模型,如下所示:
<link rel="stylesheet" href="~/UI/Skins/skin1/base.css" />
<link rel="stylesheet" href="~/UI/Skins/skin2/base.css" />
Using this system and Razor you can now switch out the Skin Path from a database or user setting and change the whole design of your website by just changing the path dynamically. 使用此系统和Razor,您现在可以从数据库或用户设置中切换皮肤路径,并通过动态更改路径来更改网站的整个设计。
The whole purpose of CSS 15 years ago was to develop both user-controlled and application-controlled style sheet "skins" for sites so you could switch out the UI look and feel separate from the application and repurpose the content independent of the data structure.....for example a printable version, mobile, audio version, raw xml, etc. CSS 15年前的全部目的是为站点开发用户控制和应用程序控制的样式表“皮肤”,这样您就可以将UI外观与应用程序分开,并重新调整内容,而不依赖于数据结构。 ....例如可打印版本,移动版,音频版,原始xml等。
By moving back now to this "old-fashioned", hard-coded path system using C# classes, rigid styles like Bootstrap, and merging the themes of sites with application code, we have gone backwards again to how websites were built in 1998. 现在回到这个“老式”的硬编码路径系统,使用C#类,刚性样式如Bootstrap,以及将网站主题与应用程序代码合并,我们又回到了1998年网站的构建方式。