Failed to load resource: 403 forbidden with .js Optimization

青春壹個敷衍的年華 提交于 2019-12-22 02:00:13

问题


I'm trying to minify my .js and .css files.

I've installed the packed Install-Package Microsoft.AspNet.Web.Optimization

When ever i active the Optimization with BundleTable.EnableOptimizations = true;

I receive this error on the client:

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

Anyone have an idea of what I'm doing wrong?

---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"
      ));
    }
}

-----------------------------I still have not got this figured out---------------------

I'm using 2013 .net and iis8 on a windows7 OS

Here is my latest error, I cannot take my solution out of debug mode, because if I do I get that error below.

    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 »

Here is the url that is created by iis8 when not in debug mode that produces the error

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

Here is the url that returns the actual .css file with out any error

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

回答1:


Just had the same issue. In my case, the solution was to give the Content bundle a different name. I think that happen because IIS intercepts the requests and treat the bundle name as a directory and since the Content folder really exists, it returns the forbidden error. So, you could rename ~/Content/themes/elevation to say ~/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"
      ));

Also, don't forget to adjust your markup/master page to use the revised bundle name, i.e.

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

Then add location directives to the web.config to allow access to the bundles:

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

Hope this helps.




回答2:


you have to keep the bundle name similar to the actual path of the resources. otherwise system cannot find resources when compiled with debug='false' or BundleTable.EnableOptimizations = true;. because system uses the bundle name to generate the link for the resources. so your bundle names should be like this -

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"
));

Edit This will work on IIS 6. However for IIS 7 or 7.5 the solution is something else. I faced the same problem when I deployed an application in IIS 7.5. The solution is to install a hotfix as discussed in ASP.NET MVC 4 on IIS 7.5, returns 404. Something to do with extensionless route mapping and ASP.NET 4.5 MVC 4 not working on Windows Server 2008 IIS 7




回答3:


Are you sure that must be actually /elevation/v=gn... but not /themes/elevation?v=gnDLBbf (with ?)?




回答4:


As mentioned in the accepted answer, the bundle name that you assigned is clashing with an actual existing folder. As an example, consider the following:

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

Will give me the same type of error noted by OP:

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

This is because the virtual path that the optimizer is trying to create (content/epic) happens to be an existing folder path in my site (I have a folder in the root called "content" and it contains a folder called "epic"). If I change my bundle path to the following:

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

The problem no longer exists because I don't have a folder called "epic2" inside my "content" folder.

Contrary to the accepted answer, I would advise against changing a bundle directory such as "~/Content/a/b/" to "~/css/a/b" because there's another potential problem that will arise if your stylesheets contain relative references to external files.

Consider my AjaxLoadAnimation.css stylesheet which contains this snippet:

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

In order to make sure the reference works for optimized and non-optimized compilation, make sure that the path for the bundle matches the path for each item in the bundle. If your stylesheets are at ~/Content/my/path, your bundle should start with ~/Content/my/path as well. To avoid OP's issue, just make sure the name ("sharedcss" in my case) doesn't clash with an existing folder.

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"));

Hopefully this saves other people from the same frustration.



来源:https://stackoverflow.com/questions/19712168/failed-to-load-resource-403-forbidden-with-js-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!