首页 文章

MVC4中的Styles.Render

提问于
浏览
365

.NET MVC4 项目中 @Styles.Render 如何运作?

我的意思是,在 @Styles.Render("~/Content/css") 哪个文件正在调用?

我的 Content 文件夹中没有名为"css"的文件或文件夹 .

7 回答

  • 435

    它调用包含在特定包中的文件,该文件在 App_Start 文件夹的 BundleConfig 类中声明 .

    在该特定情况下,对 @Styles.Render("~/Content/css") 的调用正在调用"~/Content/site.css" .

    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
    
  • 0

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

    /Content/bootstrap.css

    然后在Bundle.config中重定向到

    .Include(“〜/ Content / Bootstrap.css”)

    它不会加载CSS .

  • 8

    派对有点晚了 . 但似乎没有人提到过
    bundlingminification of StyleBundle ,所以......

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

    来电 Application_Start()

    BundleConfig.RegisterBundles(BundleTable.Bundles);
    

    反过来打电话

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

    RegisterBundles() 有效结合& minifies bootstrap.cssSite.css
    到一个文件,

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

    But ..

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

    only when debugWeb.config 中设置为 false .
    否则 bootstrap.cssSite.css 将单独提供 .
    没有捆绑,也没有缩小:

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

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

  • 0

    如App_start.BundleConfig中所定义,它只是调用

    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
    

    即使您删除该部分也没有任何反应 .

  • 0

    Polo我不会在MVC中使用Bundles有多种原因 . 它在您的情况下不起作用,因为您必须在Apps_Start文件夹中设置自定义BundleConfig类 . 当您可以在html的头部添加样式时,这没有任何意义,如下所示:

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

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

    在类中添加CSS的硬编码链接打破了UI和设计与应用程序模型分离的全部目的 . 您也不希望在c#中管理硬编码样式表路径,因为您不能再为不同的设备,主题等构建“皮肤”或单独的样式模型,如下所示:

    <link rel="stylesheet" href="~/UI/Skins/skin1/base.css" />
    <link rel="stylesheet" href="~/UI/Skins/skin2/base.css" />
    

    使用此系统和Razor,您现在可以从数据库或用户设置中切换皮肤路径,并通过动态更改路径来更改网站的整个设计 .

    CSS 15年前的全部目的是为站点开发用户控制和应用程序控制的样式表“皮肤”,这样您就可以将UI外观与应用程序分开,并重新调整内容,而不依赖于数据结构 . ....例如可打印版本,移动版,音频版,原始xml等 .

    现在回到这个“老式”的硬编码路径系统,使用C#类,刚性样式如Bootstrap,以及将网站主题与应用程序代码合并,我们又回到了1998年网站的构建方式 .

  • 0

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

相关问题