无法加载资源:403禁止访问,伴随着.js优化问题

24

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

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

每当我使用 BundleTable.EnableOptimizations = true; 激活优化时,

我在客户端收到以下错误:

Failed to load resource: the server responded with a status of 403 (Forbidden) http://localhost:22773/Content/themes/elevation/v=gnDLBbf1VVRuQDXtIYn1q0P3ICZG7oiwwgxPRbaLvqI1

有人知道我做错了什么吗?

---BundleConfig info-------------------------------

 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

您在网站上使用任何身份验证吗? - th1rdey3
是的,我目前已经登录了。 - Filling The Stack is What I DO
假设您的_content文件夹仅包含样式/图像/js脚本,请尝试在_content文件夹中放置一个名为web.config的文件,并在其中添加<system.web> <authorization> <allow users ="*" /> </authorization> </system.web> - th1rdey3
我已经添加了web.config文件,但仍然收到403禁止访问的错误。 - Filling The Stack is What I DO
请在您的问题中包含所有与Bundle相关的代码,这些代码来自于Application_start方法。 - th1rdey3
显示剩余2条评论
4个回答

46

我曾经遇到了同样的问题。在我的情况下,解决方法是给Content捆绑包取一个不同的名称。我认为这是因为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") %>

然后在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>
希望这可以帮到您。

希望这可以帮到您。


你能格式化 web config 文件吗?路径是静态的吗?是 css 还是 bundles?还是属于你调用的代码名称?例如:Styles.Render("~/css/themes/elevation"),所以路径是 "css" 吗? - user964565
路径应与捆绑代码中的路径相同。因此,如果您想使用不同的值,请在StyleBundle(或ScriptBundle,如果是脚本捆绑)中进行更改,然后在Styles.Render()/Scripts.Render()中进行更改,最后将新名称放入web.config中。希望这可以帮助到您。 - Denis Ivin

3

您需要将bundle名称与资源的实际路径保持相似。否则,在使用debug='false'BundleTable.EnableOptimizations = true;进行编译时,系统无法找到资源。因为系统使用bundle名称生成资源的链接。所以你的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,解决方案则是另一回事。当我在IIS 7.5上部署应用程序时,我也遇到了同样的问题。解决方案是安装一个热修复补丁,如ASP.NET MVC 4在IIS 7.5上返回404。与无扩展路由映射有关ASP.NET 4.5 MVC 4在Windows Server 2008 IIS 7上不起作用中所讨论的。


0

你确定应该使用/elevation/v=gn...而不是/themes/elevation?v=gnDLBbf(带有?)吗?


现在我想起来了,你说得对。如果Microsoft.AspNet.Web.Optimization没有更改路径或者Microsoft.AspNet.Web.Optimization没有将文件缓存到其他地方,那么应该是这样的。我所做的就是安装Microsoft.AspNet.Web.Optimization并将BundleTable.EnableOptimizations = true;添加到我的BundleConfig中。除此之外,我没有做任何其他事情。我错过了什么步骤吗? - Filling The Stack is What I DO
1
所以,你的意思是,当你不使用优化(或者只是将EnableOptimization = false)时,它能正常工作?如果是这样,那么即使没有'?',而是用'/',也可以工作,因为在提供的捆绑包中从未提到名为“Content/themes/elevation/v”的捆绑包,这就是为什么你会进入Content/themes/elevation/v/目录,显然会给你403的原因。 - Agat
好的。如果您使用此链接 http://localhost:22773/Content/themes/elevation?v=gnDLBbf1VVRuQDXtIYn1q0P3ICZG7oiwwgxPRbaLvqI1,您会得到什么? - Agat
嗯,这真的很奇怪。你应该以某种方式提供项目源代码...或者至少是其中的某些部分。 - Agat
让我们在聊天中继续这个讨论 - Filling The Stack is What I DO
显示剩余5条评论

0
如已在接受的答案中提到,您分配的包名称与一个实际存在的文件夹冲突。以以下示例为例:
bundles.Add(new StyleBundle("~/content/epic").Include(
    "~/Content/Epic/StickyFooter.css"));

我会得到与 OP 注意到的相同类型的错误:

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

这是因为优化器试图创建的虚拟路径(content/epic)恰好是我网站中现有的文件夹路径(我在根目录下有一个名为“content”的文件夹,其中包含一个名为“epic”的文件夹)。如果我将我的捆绑路径更改为以下内容:
bundles.Add(new StyleBundle("~/content/epic2").Include(
    "~/Content/Epic/StickyFooter.css"));

问题已经不存在了,因为我在“content”文件夹中没有名为“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"));

希望这能避免其他人遇到同样的挫折。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接