C# MVC4 中的 Styles.Render

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12028401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:07:39  来源:igfitidea点击:

Styles.Render in MVC4

c#asp.net-mvcrazorasp.net-mvc-4

提问by Ricardo Polo Jaramillo

In a .NET MVC4project how does @Styles.Renderworks?

在一个.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 Contentfolder.

我的文件夹中没有名为“css”的文件或文件Content夹。

采纳答案by NunoCarmo

It's calling the files included in that particular bundle which is declared inside the BundleConfigclass in the App_Startfolder.

它调用包含在文件夹中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"));

回答by linktoemi

Watch out for case sensitivity. If you have a file

注意区分大小写。如果你有一个文件

/Content/bootstrap.css

/内容/bootstrap.css

and you redirect in your Bundle.config to

你在你的 Bundle.config 中重定向到

.Include("~/Content/Bootstrap.css")

.Include("~/Content/Bootstrap.css")

it will not load the css.

它不会加载 css。

回答by Ram

src="@url.content("~/Folderpath/*.css")"should render styles

src="@url.content("~/Folderpath/*.css")"应该渲染样式

回答by Sathish Dadi

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.

即使您删除该部分,也不会发生任何事情。

回答by Stokely

Polo I wouldn't use Bundles in MVC for multiple reasons. It doesn't work in your case because you have to set up a custom BundleConfig class in your Apps_Start folder. This makes no sense when you can simple add a style in the head of your html like so:

出于多种原因,我不会在 MVC 中使用 Bundles。它在您的情况下不起作用,因为您必须在 Apps_Start 文件夹中设置自定义 BundleConfig 类。当您可以像这样简单地在 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. If your styles change, you can easily change the name and path without having to recompile.

您还可以将这些添加到从所有视图调用并放入每个页面的 Layout.cshtml 或部分类。如果您的样式发生变化,您可以轻松更改名称和路径,而无需重新编译。

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. 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:

在类中添加到 CSS 的硬编码链接也破坏了将 UI 和设计与应用程序模型分离的整个目的。您也不希望在 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.

15 年前 CSS 的全部目的是为站点开发用户控制和应用程序控制的样式表“皮肤”,这样您就可以将 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 年构建网站的方式。

回答by SAm

A bit late to the party. But it seems like no one has mentioned
bundling& minificationof StyleBundle, so..

聚会有点晚了。但似乎没有人提到
的捆绑微小StyleBundle,所以..

@Styles.Render("~/Content/css") 

calls in Application_Start():

调用Application_Start()

BundleConfig.RegisterBundles(BundleTable.Bundles);            

which in turn calls

反过来调用

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new StyleBundle("~/Content/css").Include(
              "~/Content/bootstrap.css",
              "~/Content/Site.css"));
}


RegisterBundles()effectively combines & minifiesbootstrap.css& Site.css
into a single file,

RegisterBundles()有效地组合 &缩小bootstrap.css& Site.css
到一个单一的文件,

<link href="/Content/css?v=omEnf6XKhDfHpwdllcEwzSIFQajQQLOQweh_aX9VVWY1" rel="stylesheet">

But..

但是..

<system.web>
  <compilation debug="false" targetFramework="4.6.1" />
</system.web>

only whendebugis set to falsein Web.config.
Otherwise bootstrap.css& Site.csswill be served individually.
Not bundled, nor minified:

仅当debug设置为falsein 时Web.config
否则,bootstrap.cssSite.css将单独提供。
不捆绑,也不缩小:

<link href="/Content/bootstrap.css" rel="stylesheet">
<link href="/Content/Site.css" rel="stylesheet">

回答by dudeNumber4

I did all things necessary to add bundling to an MVC 3 web (I'm new to the existing solution). Styles.Renderdidn't work for me. I finally discovered I was simply missing a colon. In a master page: <%: Styles.Render("~/Content/Css") %>I'm still confused about why (on the same page) <% Html.RenderPartial("LogOnUserControl"); %>works withoutthe colon.

我做了所有必要的事情来将捆绑添加到 MVC 3 web(我是现有解决方案的新手)。 Styles.Render对我不起作用。我终于发现我只是缺少一个冒号。在母版页中:<%: Styles.Render("~/Content/Css") %>我仍然对为什么(在同一页面上)没有冒号也能<% Html.RenderPartial("LogOnUserControl"); %>工作感到困惑。