在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来作为虚拟路径。

StackOverFlow原文地址

ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)的更多相关文章

  1. WebSocket在ASP.NET MVC4中的简单实现

    WebSocket在ASP.NET MVC4中的简单实现 2013-12-21 20:48 by 小白哥哥, 810 阅读, 3 评论, 收藏, 编辑 WebSocket 规范的目标是在浏览器中实现和 ...

  2. C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用

    C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备)  https://blog.csdn.net/u013519551/article/details/51220841 1. . ...

  3. 在ASP.NET MVC4中实现同页面增删改查,无弹出框02,增删改查界面设计

    在上一篇"在ASP.NET MVC4中实现同页面增删改查,无弹出框01,Repository的搭建"中,已经搭建好了Repository层,本篇就剩下增删改查的界面了......今 ...

  4. ASP.NET MVC4中的App_start中BundleConfig的介绍使用

    在BundleConfig.cs中,指定CSS和JS,主要用来压缩JS和CSS   在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压 ...

  5. ASP.NET MVC4中对JS和CSS的引用

    https://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: < ...

  6. ASP.NET MVC4中使用NHibernate

    ASP.NET MVC4中使用NHibernate 1:下载安装NHibernate 打开 VS 2012新建一个 MVC4项目. 在项目名称上右击选择Manage NuGet Packages.你会 ...

  7. Asp.Net MVC4开发二: Entity Framework在Asp.Net MVC4中的应用

    ORM作为一种数据库訪问机制已广泛地应用于各种项目其中,在.Net开发中,应用比較广泛的ORM框架大致有以下几个: 官方支持的有:Linq to SQL.Entity Framework.三方的有:N ...

  8. Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据

    Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据 在这一节中,你将新创建一个新的 MoviesController类,并编写代码,实现获取影片数据和使用视图模板在浏览器中展现 ...

  9. 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 ...

随机推荐

  1. 查询数据表,去除符合某些条件的记录,没有自动增长列(not exists)

    select distinct ccode,isnull(cexch_name,''),N'',N'',N'2014.03',0,1,1,1,12 from RP_bankrecp where not ...

  2. JS之相等操作符

    1.相等 == 和不相等 != 先转换操作数再比较相等性 在转换不同的数据类型时,相等和不相等遵循以下规则: 1.1 如果有一个操作数是布尔值,则在比较相等性前先将其转换为数值---false转换为0 ...

  3. PySe-004-Se-WebDriver 启动浏览器之二 - Chrome

    上篇文章简略讲述了 WebDriver 启动 firefox 浏览器的示例脚本源码,具体请参阅: PySe-003-Se-WebDriver 启动浏览器之一 - Firefox 此文主要讲述在 Mac ...

  4. std::map的clear()没有用?

    昨天晚上,我徒弟跑过来讲,他的程序的内存占用居高不下,愿意是std::map的clear()没有效果.于是我让他用erase(begin,end); 试试也不行. 代码如下: void release ...

  5. imx6 启动 init进程

    之前不知道imx6内核是怎么启动文件系统的init进程,查了下资料,记录于此,以后再来补充. kernel/init/main.c static noinline int init_post(void ...

  6. out 和 ref 之间的区别整理

    ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递. 对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别. 总结起来,他们有如下语法区别: 1.re ...

  7. jenkins+git实现docker持续部署

    jenkins所做的事情很简单,就拿我现在的情况来说吧,(1).每次开发完成,我都会push到我的远程仓库:(2).我再将我push到远程仓库的代码pull到我的测试服务器上:(3).在测试服务器上, ...

  8. 建字段_添加数据_生成json.php

    <?php header("Content-Type:text/html;charset=utf8"); class db{ static $localhost = &quo ...

  9. Cache封装类

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  10. javascript实例学习之一——联动下拉框

    经常碰到这样的需求,如省市_城市_乡镇下拉框.后一个下拉框的内容会根据前一个的改变而改变,之前的解决方法往往是在前一个改变的时候,向后台发送请求临时获取对应的内容.这样每触发一次就会网络请求一次,这种 ...