ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)
在ASP.NET MVC4中微软引入了bundles特性,这个特性可以将服务器端的多个Javascript或多个css文件捆绑在一起作为一个单一的URL地址供客户端浏览器调用,从而减少了页面上Http请求的访问次数,增加页面的响应速度。本文不打算介绍MVC4中的bundles特性,如果需要了解,推荐可以查看下面这位博主的文章:
在ASP.NET MVC中,使用Bundle来打包压缩js和css
本文要说的问题是当你使用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来作为虚拟路径。
ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)的更多相关文章
- WebSocket在ASP.NET MVC4中的简单实现
WebSocket在ASP.NET MVC4中的简单实现 2013-12-21 20:48 by 小白哥哥, 810 阅读, 3 评论, 收藏, 编辑 WebSocket 规范的目标是在浏览器中实现和 ...
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- 在ASP.NET MVC4中实现同页面增删改查,无弹出框02,增删改查界面设计
在上一篇"在ASP.NET MVC4中实现同页面增删改查,无弹出框01,Repository的搭建"中,已经搭建好了Repository层,本篇就剩下增删改查的界面了......今 ...
- ASP.NET MVC4中的App_start中BundleConfig的介绍使用
在BundleConfig.cs中,指定CSS和JS,主要用来压缩JS和CSS 在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压 ...
- ASP.NET MVC4中对JS和CSS的引用
https://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: < ...
- ASP.NET MVC4中使用NHibernate
ASP.NET MVC4中使用NHibernate 1:下载安装NHibernate 打开 VS 2012新建一个 MVC4项目. 在项目名称上右击选择Manage NuGet Packages.你会 ...
- Asp.Net MVC4开发二: Entity Framework在Asp.Net MVC4中的应用
ORM作为一种数据库訪问机制已广泛地应用于各种项目其中,在.Net开发中,应用比較广泛的ORM框架大致有以下几个: 官方支持的有:Linq to SQL.Entity Framework.三方的有:N ...
- Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据
Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据 在这一节中,你将新创建一个新的 MoviesController类,并编写代码,实现获取影片数据和使用视图模板在浏览器中展现 ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\d29b5393\123c3a1c\App_Code.odl3w4o6.dll”--“拒绝访问。 ”
IIS部署网站或者Webservice时,出现以下问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...
随机推荐
- 在Fedora8上配置Tomcat6.0.37
说是配置,其实很简单,解压拷贝是主要的活动,主要还是Tomcat做得很好很方便. 安装Tomcat的前提是把JDK装好,这和Win下是一样的.下面是整个步骤: 第一步,到Tomcat官网下载apach ...
- 替换SearchBar 键盘上的 搜索 按钮
for (UIView *searchBarSubview in [searchBar subviews]) { if ([searchBarSubview conformsToProt ...
- Oracle直方图导致SQL不走索引.
在ITPUB 上看到一个帖子 http://www.itpub.net/thread-1875212-1-1.html 同一条SQL语句,只有查询条件不一样,查询返回的结果集都为0,一个走了全表扫描, ...
- JavaScript:window窗口对象
在JavaScript中,window表示的就是一个窗口对象.所以在整个处理过程之中,所有的操作都是以弹框为主 的.范例1:使用警告框 <script type="text/javas ...
- winston 日志管理4
配置File Transport winston.add(winston.transports.File, options) The File transport should really be t ...
- activity传值到fragment
1,初始化fragment时候: @Override public void onAttach(Activity activity) { /** 注册广播 */ initBroadcast(); s ...
- 第六篇 Replication:合并复制-发布
本篇文章是SQL Server Replication系列的第六篇,详细内容请参考原文. 合并复制,类似于事务复制,包括一个发布服务器,一个分发服务器和一个或多个订阅服务器.每一个发布服务器上可以定义 ...
- myeclipse WIN7下设置字体列表中无Courier New
到"C:\Windows\Fonts"下找到对应的字体,单击,选择上面的显示 然后就可以在eclicpse字体设置里面看到Courier New项了: Eclipse字体设置方法: ...
- iOS 事件穿透
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- linux:档案与档案系统的压缩、打包与备份
压缩比:压缩后与压缩的档案锁占用的磁碟空间大小,就称之为压缩比 压缩技术: a.将没有使用到的空间丢出去,以让档案资料占用的空间变小 b.将重复的资料统计记录(比如100个1,不是真正的用100个元位 ...