【问题标题】:How to include JavaScript and CSS from custom directories in ASP.NET MVC 4, .net 4.5?如何在 ASP.NET MVC 4、.net 4.5 中包含自定义目录中的 JavaScript 和 CSS?
【发布时间】:2012-08-27 12:15:27
【问题描述】:

考虑到这个文件夹结构:

Content
     style1.css
     style2.css
     CssFolder1
         style3.css
         style4.css
         style5.css
         style6.css

在应用程序中,我想包含style1.cssstyle2.cssstyle3.cssstyle4.css

我正在这样做:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new StyleBundle("~/styles/main").Include("~/Content/style1.css",   "~/Content/style2.css"));
        bundles.Add(new StyleBundle("~/styles/custom").Include("~/Content/CssFolder1/style3.css","~/Content/CssFolder1/style4.css"));
    }
}

_Layout.cshtml 中,我将其渲染为:

@Styles.Render("~/styles/main", "~/styles/custom")

仅加载 style1.css 和 style2.css。如何正确加载style3.cssstyle4.css

LE:内容是解决方案中的一个文件夹,该文件夹中的所有文件都将添加到解决方案中。

【问题讨论】:

  • 您的代码看起来不错。您使用的是哪个网络服务器 VS 开发服务器、IIS express、IIS?你确定文件在那里吗?如果您使用<link href="@Url.Content("~/Content/CssFolder1/style3.css")" rel="stylesheet" type="text/css" /> 手动包含它们会发生什么?
  • 我已经尝试过了,如果我手动包含它们,它们就会被加载。我正在使用 VS 开发服务器。
  • 您是否使用表单身份验证?如果是这样,您是否允许公开访问样式/自定义?

标签: asp.net-mvc .net-4.5 asp.net-optimization bundling-and-minification


【解决方案1】:

所有文件都需要添加到 Visual Studio 解决方案窗口中的项目中。右键单击css 文件夹,然后单击add existing item。导航到 styles3.css 并以这种方式添加。

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 2010-09-12
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多