在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. Macro and SQL

    If you’ve developed anything in the supply chain area, you’ve most probably come across InventDimJoi ...

  2. 经典Bug 修改方法

    右击工程名,“显示包内容”,删除提示错误的所有相关内容,(想当初,仅仅是删了一个图片,就各种报错,clean也没用,删了重新运行,也不成功.....有能力的话,最好能FQ,google你会发现很多技巧 ...

  3. Mac版 MicrosoftOffice2015 办公软件 破解教程

    来自:http://bbs.feng.com/read-htm-tid-9704285.html 下载链接:http://pan.baidu.com/s/1dD6lBFz  提取密码:xu5n 补丁: ...

  4. Naming Conventions for .NET / C# Projects

    http://www.akadia.com/services/naming_conventions.html Naming Conventions for .NET / C# Projects Mar ...

  5. 用Intellij IDEA 创建第一个maven项目!

    1. 一直想如何复用以前项目的maven的jar包! 其实只要拿到pom.xml即可!!! 1.1 创建一个maven项目 2. 3. 创建项目名和项目路径,我给项目起的名字是mavenV1.0 4. ...

  6. Mysqldump 参数详解(全)

    Mysqldump 参数详解(全) http://www.open-open.com/lib/view/open1358172843762.html mysqldump -S /tmp/mysql33 ...

  7. 8款实用Sublime text 3插件推荐

    Sublime Text作为一个尽为人知的代码编辑器,其优点不用赘述.界面整洁美观.文本功能强大,且运行速度极快,非常适合编写代码,写文章做笔记.Sublime Text还支持Mac.Windows和 ...

  8. 开发API文档相关问题(*.chm)

    在网上下了一个chm的文件,打开后只有目录,右侧不显示内容. 不知道是文件有问题,还是系统有问题. google了一下.解决了. 右键点击文件–属性 看到 最下面有一个提示 说是这个文件是来自其他计算 ...

  9. 让SpringMVC支持可版本管理的Restful接口

    需求 移动互联网时代的到来,软件开发的模式也在变化.记得以前做B/S的后台开发,基本上没有Http接口一说,全部是通过渲染模板技术(jsp,freemark)把最终html展示给最终用户.现在完全变了 ...

  10. Java jaxp查询节点

    <?xml version="1.0" encoding="UTF-8"?> <person> <p1> <name& ...