C# 无法加载资源:403禁止使用.js优化

C# 无法加载资源:403禁止使用.js优化,c#,.net,optimization,minify,C#,.net,Optimization,Minify,我正在尝试缩小我的.js和.css文件 我已经安装了打包的安装包Microsoft.AspNet.Web.Optimization bundles.Add(new StyleBundle("~/css/themes/elevation").Include( "~/Content/themes/dialogs/dialogs.css", "~/Content/themes/social/ac/acSocial.css", "~

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

我已经安装了打包的
安装包Microsoft.AspNet.Web.Optimization

bundles.Add(new StyleBundle("~/css/themes/elevation").Include(
            "~/Content/themes/dialogs/dialogs.css",
            "~/Content/themes/social/ac/acSocial.css",
            "~/Content/themes/elevation/elevation.css"
      ));
当我使用
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"
      ));
    }
}
-----------------------------我还没弄明白---------------------

 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
http://localhost:1499/Content/themes/elevation/elevation.css
这里是返回实际.css文件的url,没有任何错误

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

您确定必须是/elevation/v=gn。。。但不是/themes/elevation?v=gnDLBbf(使用)?

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

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,解决方案是另一回事。我在IIS7.5中部署应用程序时也遇到了同样的问题。解决方案是安装热修复程序,如中所述,

刚刚遇到了相同的问题。在我的例子中,解决方案是给内容包一个不同的名称。我认为之所以会发生这种情况,是因为IIS拦截请求并将捆绑包名称视为一个目录,而且由于内容文件夹确实存在,它会返回禁止的错误。因此,您可以将
~/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") %>

然后将位置指令添加到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>


希望这能有所帮助。

如接受的答案中所述,您指定的捆绑包名称与实际存在的文件夹冲突。作为一个例子,考虑如下:

bundles.Add(new StyleBundle("~/content/epic").Include(
    "~/Content/Epic/StickyFooter.css"));
bundles.Add(new StyleBundle("~/content/epic2").Include(
    "~/Content/Epic/StickyFooter.css"));
将给我OP指出的相同类型的错误:

{myURL}/content/epic/?v=YTZL7Up6r-0uQblkv6unjKN5Nfb3uwtE0bPz9nxbjDc1 Failed to load 
这是因为优化器试图创建的虚拟路径(content/epic)恰好是我站点中的现有文件夹路径(我在根目录中有一个名为“content”的文件夹,它包含一个名为“epic”的文件夹)。如果我将捆绑路径更改为以下路径:

bundles.Add(new StyleBundle("~/content/epic").Include(
    "~/Content/Epic/StickyFooter.css"));
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"));

希望这能让其他人免于同样的挫折。

您是否在网站上使用任何身份验证?是的,我目前已登录。假设您的内容文件夹仅包含样式/图像/js脚本,尝试将
web.config
文件放置在内容文件夹中,其中
包含在
Global.asax
应用程序启动
方法中。我已经添加了web.config文件,我仍然得到了403个禁止。我现在正在考虑这个问题,也就是说,如果Microsoft.AspNet.Web.Optimization没有更改路径,或者Microsoft.AspNet.Web.Optimization没有在其他地方缓存文件。我所做的只是安装了Microsoft.AspNet.Web.Optimization并添加了BundleTable.EnableOptimizations=true;给我的朋友。除此之外,我没有做过任何事。我错过了一个步骤吗?那么,你的意思是,当你不使用优化时(或者如果你只是设置EnableOptimization=false),它工作得很好?如果是这样的话,它甚至可以在没有“?”的情况下工作,这很奇怪,但是使用“/”就可以了,因为在您提供的捆绑包中从来没有提到过名为“Content/themes/elevation/v”的捆绑包,这就是为什么您进入Content/themes/elevation/v/目录的原因,它显然会给您403。是否有一个我应该设置的地方来解决这个问题?我看得太多了?好的。如果改用这个,你会得到什么?我收到这个HTTP错误403.14-禁止你可以格式化web配置吗?路径是静态的?css还是捆绑包?还是属于你呼叫的代码名?比如:Styles.Render(“~/css/themes/elevation”),所以路径是“css”?路径应该与绑定代码中的路径相同。因此,如果您想使用不同的值,请在StyleBundle(或者脚本包中的ScriptBundle)中更改它,然后在Styles.Render()/Scripts.Render()中更改它,最后将新名称放入web.config。希望这有帮助。