zoukankan      html  css  js  c++  java
  • ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)

    当你使用bundles.Add方法添加StyleBundle和ScriptBundle对象的时候一定要注意,StyleBundle和ScriptBundle的构造函数的参数virtualPath指定的虚拟路径一定不能是当前ASP.NET项目中真实存在的一个文件夹路径,否则当你把你的站点部署到IIS上后,会发现MVC页面上用@Styles.Render和@Scripts.Render生成的url路径会被IIS拒绝,IIS提示 禁止访问 403错误。

    下面是有个老外遇到了相同的问题,在StackOverFlow论坛上的提问:

    My MVC 4 application works fine on my local computer.

    However, the CSS files are not working after I publish (are not affecting the layout of the website).

    I can see CSS the files are on the server.

    When I look at the source code, I can see

    <link href="/Content/css?v=fxCdHAOgPDvcROxkMfEwGQggO9uCfzckN3PaN8BOIzI1" rel="stylesheet"/>

    where as on my local computer, the source code shows as

    <link href="/Content/css/myFile.css" rel="stylesheet"/>
    <link href="/Content/css/myFile02.css" rel="stylesheet"/>

    So, in the source code view on the server, I clicked on  Content/css?v=fxCdHAOgPDvcROxkMfEwGQggO9uCfzckN3PaN8BOIzI1 and the browser took me to a 403 - Forbidden: Access is denied.

    I am adding the CSS files with the BunldeConfig.cs class

    复制代码
    public class BundleConfig
        {
            // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
            public static void RegisterBundles(BundleCollection bundles)
            {
                bundles.Add(new ScriptBundle("~/bundles/javascript").Include(
                            "~/Scripts/1.9.1.js",
                            "~/Scripts/jTools.js",
                            "~/Scripts/script.js"
                            ));
    
                bundles.Add(new StyleBundle("~/Content/css").Include(
                    "~/Content/Css/website.css", 
                    "~/Content/Css/banner.css", 
                    "~/Content/Css/reusable.css",
                    "~/Content/Css/lists.css",
                    "~/Content/Css/tooltip.css",
                    "~/Content/Css/overlay.css"
                    ));
    
    
            }
        }
    复制代码

    My question is, assuming this isn't an IT issue with the server (it has been working fine until recently) is there something wrong with my code?

    StackOverFlow论坛上专家的回答:

    Your problem is that you are using ~/Content/css as a bundle alias in new StyleBundle("~/Content/css"), while this path actually exists.

    So when you are requesting <link href="/Content/css?...> you are essentially asking for a directory listing and that is forbidden.

    Try using something else, like new StyleBundle("~/Content/styles").

    NOTE: If you do use something like ~/Content/styles as an alias you may have issues with relative urls in your .css files. It may seem odd, but you may better use something like ~/Content/Css/someAlias

    也就是说构造StyleBundle和ScriptBundle时定义的虚拟路径不能是服务器上真实存在的物理路径,同时上面这位专家还提到StyleBundle的虚拟路径最好也不要使用~/Content/styles,因为这可能会使.css文件的访问出现问题,对于StyleBundle最好用~/Content/Css/someAlias来作为虚拟路径。

    StackOverFlow原文地址

  • 相关阅读:
    linux 解压tgz 文件指令
    shell 脚本没有执行权限 报错 bash: ./myshell.sh: Permission denied
    linux 启动solr 报错 Your Max Processes Limit is currently 31202. It should be set to 65000 to avoid operational disruption.
    远程查询批量导入数据
    修改 MZTreeView 赋权节点父节点选中子节点自动选中的问题
    关于乱码的问题解决记录
    我的网站优化之路
    对设计及重构的一点反思
    我的五年岁月
    奔三的路上
  • 原文地址:https://www.cnblogs.com/micenote/p/7236990.html
Copyright © 2011-2022 走看看