MVC4中的Styles.Render

本文翻譯自: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年網站的構建方式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章