asp.net-mvc ASP.NET MVC 框架 4.5 CSS 包在托管上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12081255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC framework 4.5 CSS bundle does not work on the hosting
提问by Ricardo Polo Jaramillo
I am runing an App on app harbor written in MVC4.
我正在用 MVC4 编写的应用程序港上运行一个应用程序。
A bundle of css files doesn't work. In my local computer in debug mode I see the code of the App and I see the files. The App works as expected.
一堆 css 文件不起作用。在调试模式下的本地计算机中,我看到了应用程序的代码和文件。该应用程序按预期工作。
<link href="/Content/css/home/basic-jquery-slider.css" rel="stylesheet"/>
<link href="/Content/css/home/Home.css" rel="stylesheet"/>
When I upload the app to Appharbor I see the bundle in the code but the App doesn't work.
当我将应用程序上传到 Appharbor 时,我在代码中看到了捆绑包,但该应用程序不起作用。
<link href="/Content/css/home?v=zhVOIpUNuvCOZhJyBcQWpMlozayor4te6k-pM29wHqI1" rel="stylesheet"/>
When I browse that link in the hrefI get 403 - Forbidden: Access is denied.
当我浏览的那个链接href,我得到403 -禁止访问:访问被拒绝。
How to troubleshoot this?
如何解决这个问题?
回答by Paul
My guess is that the path Content/cssexists on disk in your app. In this case IIS would be handling the request, not MVC.
我的猜测是该路径Content/css存在于您的应用程序的磁盘上。在这种情况下,IIS 将处理请求,而不是 MVC。
Make sure that the virtual path for the bundle (the parameter of the StyleBundle constructor) doesn't match a folder in the file system.
确保包的虚拟路径(StyleBundle 构造函数的参数)与文件系统中的文件夹不匹配。
From the comments:
来自评论:
"A good convention to follow when creating bundles is to include "bundle" as a prefix in the bundle name. This will prevent a possible routing conflict."
“创建捆绑包时要遵循的一个很好的约定是在捆绑包名称中包含“捆绑包”作为前缀。这将防止可能的路由冲突。”
回答by cdeutsch
This issue is by default .NET does not "process" requests that have a .js or .css extension.
此问题是默认情况下 .NET 不会“处理”具有 .js 或 .css 扩展名的请求。
There are two fixes for this (you only need to do ONE)
对此有两个修复程序(您只需要执行一个)
A) Remove the extensions from the bundle names. (recommended) This will cause .NET to process the request and run it through the BundleModule.
A) 从包名称中删除扩展名。(推荐)这将导致 .NET 处理请求并通过 BundleModule 运行它。
B) Add this to your web.config in the system.webServersection which will cause .NET to run .js and .css requests through the BundleModule.
B) 将此添加到system.webServer部分的web.config 中,这将导致 .NET 通过 BundleModule 运行 .js 和 .css 请求。
<modules runAllManagedModulesForAllRequests="true">
<remove name="BundleModule" />
<add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>
Big shout out to Ray Moro who figured out the actual cause and shared it with me on my blog: http://blog.cdeutsch.com/2012/11/fixing-404-errors-for-aspnet-mvc-apps.html
向 Ray Moro 大喊大叫,他找出了真正的原因并在我的博客上与我分享:http: //blog.cdeutsch.com/2012/11/fixing-404-errors-for-aspnet-mvc-apps.html
回答by Yasser Shaikh
Happened with me too, when I tried deploying my ASP.NET MVC app on AppHarbor.
当我尝试在 AppHarbor 上部署我的 ASP.NET MVC 应用程序时,我也发生了这种情况。
I had a stylesheet bundle with the name
我有一个带有名称的样式表包
@Styles.Render("~/Content/bootstrap")
and the folder structure was
文件夹结构是
-- Content
-- Content \ Bootstrap \ ...
- 内容
-- 内容 \ Bootstrap \ ...
By just changing the bundle name to "~/Content/bootstrap-css"my issue got resovled.
只需将捆绑包名称更改为"~/Content/bootstrap-css"我的问题即可解决。
回答by RayLoveless
I know I'm 4 years late to this question but this worked for me.
我知道我对这个问题晚了 4 年,但这对我有用。
public static void RegisterBundles(BundleCollection bundles)
{
...
BundleTable.EnableOptimizations = true; // Added this
}
回答by yantaq
403 error solved.here is a detailed explanation and solution for 403 error.
The solutionis demonstrated for CSS bundle. However it also applies to JavaScript.
403 错误已解决。这是403错误的详细解释和解决方案。
该解决方案针对 CSS 包进行了演示。但是它也适用于 JavaScript。
http://www.mvccentral.net/Story/Details/articles/kahanu/stylebundle-403-error-solved
http://www.mvccentral.net/Story/Details/articles/kahanu/stylebundle-403-error-solved
In a nutshell, make sure that the virtual path [Script | Style]Bundle("~/content/[script | css]")doesn't match a folder in the file system (e.g. C:\approot\Content\[script | css]) instead [Script | Style]Bundle("~/content/[scriptDiff | cssDiff]")
简而言之,请确保虚拟路径 [Script | Style]Bundle("~/content/[script | css]")与文件系统中的文件夹不匹配(例如C:\approot\Content\[script | css])[Script | Style]Bundle("~/content/[scriptDiff | cssDiff]")
回答by Ye Yint
I solved the issue by adding below line of code at BundleConfig class
我通过在 BundleConfig 类中添加以下代码行解决了这个问题
BundleTable.EnableOptimizations = false;
回答by abelabbesnabi
What I do is very simple,
我做的很简单,
I add "js" at the end of ScriptBundle like so: new ScriptBundle("~/bundles/appjs") And I add "css" at the end of StyleBundle like so: new StyleBundle("~/content/appcss")
我在 ScriptBundle 的末尾添加“js”,如下所示: new ScriptBundle("~/bundles/appjs") 我在 StyleBundle 的末尾添加“css”,如下所示: new StyleBundle("~/content/appcss")
My folder names never end with "js" or "css".
我的文件夹名称永远不会以“js”或“css”结尾。
That should do it.
那应该这样做。
回答by William
This applies to the 'ScriptBundle class' too, ensure that the 'parameter name' to the constructor does not match a path in web application file system. Remember that IIS will try to serve up the file/request.
这也适用于“ScriptBundle 类”,确保构造函数的“参数名称”与 Web 应用程序文件系统中的路径不匹配。请记住,IIS 将尝试提供文件/请求。
回答by Greg Gum
The problem can also come from the file being encrypted. This occurred to me when I downloaded BootStrap and used the supplied files. They showed green in Windows explorer and worked fine in Visual Studio, but when deployed, I got a 403 error.
问题也可能来自被加密的文件。当我下载 BootStrap 并使用提供的文件时,我想到了这一点。它们在 Windows 资源管理器中显示为绿色,在 Visual Studio 中运行良好,但在部署时出现 403 错误。
You can see if they are encrypted by going to properties, then advanced properties and there is an encypted checkbox.
您可以通过转到属性、高级属性和加密复选框来查看它们是否已加密。
Uncheck and it will no longer be a problem.
取消选中它就不再是问题了。
回答by Konstantin Orlov
I have the same problem with that (403 Forbidden) error. In my case the reason is proxy server in my organization blocks my css file. Css file name matches one of block rules.
我对那个(403 Forbidden)错误有同样的问题。就我而言,原因是我组织中的代理服务器阻止了我的 css 文件。CSS 文件名匹配块规则之一。

