24

我正在尝试缩小我的 .js 和 .css 文件。

我已经安装了打包好的Install-Package Microsoft.AspNet.Web.Optimization

当我激活优化时BundleTable.EnableOptimizations = true;

我在客户端收到此错误:

加载资源失败:服务器响应状态为 403(禁止)http://localhost:22773/Content/themes/elevation/v=gnDLBbf1VVRuQDXtIYn1q0P3ICZG7oiwwgxPRbaLvqI1

任何人都知道我做错了什么?

---BundleConfig 信息-------------------------------

 public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        BundleTable.EnableOptimizations = true;

        bundles.Add(new ScriptBundle("~/bundles/myJquery").Include(

           "~/Scripts/jquery-1.9.1.js",
          "~/Scripts/jquery-ui-1.10.1.custom.js",
            "~/Scripts/jquery.signalR-1.0.1.js",
            "~/Scripts/signalr-hubs.js",
            "~/Scripts/Controls/Select/Simple/jquery.ui.selectmenu.js"
        ));


        bundles.Add(new ScriptBundle("~/bundles/shared").Include(
            "~/Scripts/global/prototypes.js",
            "~/Scripts/global/mathutil.js",
            "~/Scripts/global/elevationevents.js"
            ));


        bundles.Add(new ScriptBundle("~/bundles/core").Include(
            "~/Scripts/elevation/core/sys.config.js",
            "~/Scripts/elevation/core/bays.js",
            "~/Scripts/elevation/core/door.js",
            "~/Scripts/elevation/core/horiziontal.js",
            "~/Scripts/elevation/core/vertical.js"));


        bundles.Add(new StyleBundle("~/Content/themes/elevation").Include(
            "~/Content/themes/dialogs/dialogs.css",
            "~/Content/themes/social/ac/acSocial.css",
            "~/Content/themes/elevation/elevation.css"
      ));
    }
}

-----------------我仍然没有弄清楚这一点------------- --------

我在 windows7 操作系统上使用 2013 .net 和 iis8

这是我最新的错误,我无法将我的解决方案退出调试模式,因为如果这样做,我会在下面收到该错误。

    HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.

Things you can try:
If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists.
Enable directory browsing.
Go to the IIS Express install directory.
Run appcmd set config /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the server level.
Run appcmd set config ["SITE_NAME"] /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the site level.
Verify that the configuration/system.webServer/directoryBrowse@enabled attribute is set to true in the site or application configuration file.

Detailed Error Information:
Module     DirectoryListingModule
Notification       ExecuteRequestHandler
Handler    StaticFile
Error Code     0x00000000
Requested URL      http://localhost:1499/Content/themes/elevation/?v=aukmuLTC3g_fDko3eWmzqq7A8miRqgsJKXA2GO3w-pg1
Physical Path      c:\users\administrator\documents\visual studio 2013\Projects\AlumCloud\AlumCloud\Content\themes\elevation\
Logon Method       Anonymous
Logon User     Anonymous
Request Tracing Directory      C:\Users\Administrator\Documents\IISExpress\TraceLogFiles\ALUMCLOUD(3)

More Information:
This error occurs when a document is not specified in the URL, no default document is specified for the Web site or application, and directory listing is not enabled for the Web site or application. This setting may be disabled on purpose to secure the contents of the server.
View more information »

这是 iis8 在不处于调试模式时创建的 url,它会产生错误

http://localhost:1499/Content/themes/elevation/?v=aukmuLTC3g_fDko3eWmzqq7A8miRqgsJKXA2GO3w-pg1

这是返回实际 .css 文件且没有任何错误的 url

http://localhost:1499/Content/themes/elevation/elevation.css
4

4 回答 4

46

只是有同样的问题。就我而言,解决方案是给内容包一个不同的名称。我认为发生这种情况是因为 IIS 拦截了请求并将包名称视为目录,并且由于 Content 文件夹确实存在,因此它返回了禁止的错误。所以,你可以重命名~/Content/themes/elevation~/css/themes/elevation

bundles.Add(new StyleBundle("~/css/themes/elevation").Include(
            "~/Content/themes/dialogs/dialogs.css",
            "~/Content/themes/social/ac/acSocial.css",
            "~/Content/themes/elevation/elevation.css"
      ));

另外,不要忘记调整您的标记/母版页以使用修改后的包名称,即

<%: Styles.Render("~/css/themes/elevation") %>

然后将 location 指令添加到 web.config 以允许访问包:

<location path="css">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="bundles">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

希望这可以帮助。

于 2014-01-26T22:06:28.820 回答
3

您必须保持包名称与资源的实际路径相似。debug='false'否则,使用or编译时系统无法找到资源BundleTable.EnableOptimizations = true;。因为系统使用捆绑名称来生成资源的链接。所以你的捆绑包名称应该是这样的 -

bundles.Add(new ScriptBundle("~/Scripts/myJquery").Include(
    "~/Scripts/jquery-1.9.1.js",
    "~/Scripts/jquery-ui-1.10.1.custom.js",
    "~/Scripts/jquery.signalR-1.0.1.js",
    "~/Scripts/signalr-hubs.js",
    "~/Scripts/Controls/Select/Simple/jquery.ui.selectmenu.js"
));

bundles.Add(new ScriptBundle("~/Scripts/global/shared").Include(
    "~/Scripts/global/prototypes.js",
    "~/Scripts/global/mathutil.js",
    "~/Scripts/global/elevationevents.js"
));

bundles.Add(new ScriptBundle("~/Scripts/elevation/core/core").Include(
    "~/Scripts/elevation/core/sys.config.js",
    "~/Scripts/elevation/core/bays.js",
    "~/Scripts/elevation/core/door.js",
    "~/Scripts/elevation/core/horiziontal.js",
    "~/Scripts/elevation/core/vertical.js"
));

bundles.Add(new StyleBundle("~/Content/themes/dialogs/dialog").Include(
    "~/Content/themes/dialogs/dialogs.css"
));

bundles.Add(new StyleBundle("~/Content/themes/social/ac/ac").Include(
    "~/Content/themes/social/ac/acSocial.css"
));

编辑 这将适用于 IIS 6。但是对于 IIS 7 或 7.5,解决方案是另外一回事。当我在 IIS 7.5 中部署应用程序时,我遇到了同样的问题。解决方案是在 IIS 7.5 上安装 ASP.NET MVC 4 中讨论的修补程序,返回 404。与无扩展路由映射ASP.NET 4.5 MVC 4 在 Windows Server 2008 IIS 7 上不起作用有关

于 2013-11-03T17:27:20.017 回答
0

你确定那一定是 /elevation/v=gn... 而不是 /themes/elevation?v=gnDLBbf (带?)?

于 2013-11-03T16:03:59.817 回答
0

如已接受的答案中所述,您分配的捆绑包名称与实际现有文件夹冲突。例如,考虑以下情况:

bundles.Add(new StyleBundle("~/content/epic").Include(
    "~/Content/Epic/StickyFooter.css"));

会给我OP指出的相同类型的错误:

{myURL}/content/epic/?v=YTZL7Up6r-0uQblkv6unjKN5Nfb3uwtE0bPz9nxbjDc1 Failed to load 

这是因为优化器尝试创建的虚拟路径(内容/史诗)恰好是我网站中的现有文件夹路径(我在根目录中有一个名为“内容”的文件夹,它包含一个名为“史诗”的文件夹) . 如果我将捆绑路径更改为以下内容:

bundles.Add(new StyleBundle("~/content/epic2").Include(
    "~/Content/Epic/StickyFooter.css"));

问题不再存在,因为我的“内容”文件夹中没有名为“epic2”的文件夹。

与接受的答案相反,我建议不要将诸如“~/Content/a/b/”之类的捆绑目录更改为“~/css/a/b”,因为如果您的样式表包含相对引用,则会出现另一个潜在问题到外部文件。

考虑我的 AjaxLoadAnimation.css 样式表,其中包含以下代码段:

...
background: rgba( 255, 255, 255, .5 ) url('images/spin.gif') 50% 50% no-repeat;
...

为了确保参考适用于优化和非优化编译,请确保包的路径与包中每个项目的路径匹配。如果您的样式表位于 ~/Content/my/path,那么您的包也应该以 ~/Content/my/path 开头。为避免 OP 的问题,只需确保名称(在我的情况下为“sharedcss”)不会与现有文件夹冲突。

bundles.Add(new StyleBundle("~/Content/my/path/sharedcss").Include(
    "~/Content/my/path/bootstrap.css",
    "~/Content/my/path/font-awesome.css",
    "~/Content/my/path/AjaxLoadAnimation.css"));

希望这可以使其他人免于遭受同样的挫败感。

于 2017-02-08T21:47:08.010 回答