ASP.NET MVC4中的bundles特性引發伺服器拒絕訪問(403錯誤)

來源:互聯網
上載者:User

標籤:else   rect   scripts   add   方法   erro   script   com   title   

當你使用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原文地址

ASP.NET MVC4中的bundles特性引發伺服器拒絕訪問(403錯誤)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.