简体   繁体   English

MVC CSS无法在Visual Studio中呈现调试时

[英]MVC CSS not rendering in Visual Studio When debugging

I have an issue where the CSS is not rendering properly when I compile the MVC project and view it on [https://localhost/MyApp.] The buttons, and background image are not showing up. 我有一个问题,当我编译MVC项目并在[https:// localhost / MyApp上查看]时,CSS无法正确呈现。按钮和背景图像没有显示。 It worked one time, then for some reason it stop working. 它工作了一次,然后由于某种原因它停止工作。 Something with the pages not caching? 页面没有缓存的东西? I used firebug to check to see if the pages were missing, and no errors were found. 我用firebug检查页面是否丢失,没有发现错误。 Something in Visual Studio 2010 settings need changing or IIS? Visual Studio 2010中的某些设置需要更改还是IIS?

However, when I publish it to an individual website, instead of in the (default web site) area, using [https://localhost:444] website I setup in IIS 7.5, the css seem to render fine. 但是,当我将它发布到一个单独的网站,而不是在(默认网站)区域,使用我在IIS 7.5中设置的[https:// localhost:444]网站时,css似乎渲染得很好。

What is the problem? 问题是什么?

I have just been battling with the same problem - images, scripts and css not being found or rendered. 我一直在与同样的问题作斗争 - 图像,脚本和CSS没有找到或呈现。 (Visual Studio 2013, Windows 8.1. Project moved across from Visual Studio 2010.) (Visual Studio 2013,Windows 8.1。项目从Visual Studio 2010开始。)

The problem was caused by a line in Web.config: 问题是由Web.config中的一行引起的:

<mimeMap fileExtension=".mp4" mimeType="video/mp4" />

It seems that because the IIS Express application.host file already contained this mimeMap definition, IIS Express couldn't cope with it being defined again. 似乎因为IIS Express application.host文件已包含此mimeMap定义,IIS Express无法再次处理它。

Removing this line from Web.config completely solved the problem. 从Web.config中删除此行完全解决了这个问题。

One common problem that occurs on MVC 4 websites is difference between release and debug when you have css bundles. 在MVC 4网站上发生的一个常见问题是,当您拥有css包时,发布和调试之间存在差异。 It does not have to be your case, but you have the symptoms. 它不一定是你的情况,但你有症状。

I will explain on an example. 我将以一个例子来解释。

If you have bundle which looks like this: 如果你有一个看起来像这样的包:

var styles = new StyleBundle("~/Content/css").Include(
            "~/Content/toastr.css",
            "~/Content/flags/stylesheets/flags16.css",
            "~/Content/flags/stylesheets/flags32.css",
            "~/Content/Site.css")

And in flags16.css you have: 在flags16.css中你有:

background:url('flags/images/flags16.png');

Which uses file in ~Content/flags/images/flags16.png 其中使用~Content/flags/images/flags16.png

That will work in release mode (compilation debug="false" in web.config), because all of your css files in bundle will be served as 1 file located at ~/Content/css with relative path 'flags/images/flags16.png' pointing at correct file. 这将在发布模式下工作(在web.config中编译debug =“false”),因为bundle中的所有css文件将作为1个文件提供,位于~/Content/css ,相对路径为'flags/images/flags16.png'指着正确的文件。 However, in debug mode, ASP.NET MVC will disable minification, and if you used @Styles.Render("~/Content/css") inside your view, it will render link to every one of your files contained in a bundle, so there will be a: 但是,在调试模式下,ASP.NET MVC将禁用缩小,如果在视图中使用@Styles.Render("~/Content/css") ,它将呈现链接中包含的每个文件的链接,所以会有一个:

<link href="/WorkOrders.Web/Content/flags/stylesheets/flags16.css" rel="stylesheet">

And from that path, relative path to image is not ok, so images in flags16.png will not be rendered. 从该路径开始,相对于图像的路径不正常,因此不会渲染flags16.png中的图像。

One way to solve this, you need to move your .css file which contains references to images to the location where bundle is pointing (~/Content in this case), so it will have same path when it is served minified and raw. 解决此问题的一种方法是,您需要将包含对图像的引用的.css文件移动到bundle指向的位置(在这种情况下为〜/ Content),因此在服务于minified和raw时它将具有相同的路径。

UPDATE As your app is not mvc4, and you have problems when your app is not in the root of your web site (ie when it is in localhost/myapp) then you need to check paths in references to your pictures. 更新由于您的应用程序不是mvc4,并且当您的应用程序不在您的网站的根目录(即它在localhost / myapp中)时您遇到问题,那么您需要检查对图片的引用中的路径。 It is possible that you referenced them absolutely ( '/somepath/mypic.png' ), and when your app is in localhost/MyApp , path needs to be localhost/MyApp/somepath/mypic.png . 你可能绝对引用它们( '/somepath/mypic.png' ),当你的应用程序在localhost/MyApp ,路径需要是localhost/MyApp/somepath/mypic.png You can solve that by replacing path with @Url.Content(~/somepath/mypic.png) , if you are using it from cshtml. 你可以通过用@Url.Content(~/somepath/mypic.png)替换路径来解决这个问题,如果你是从cshtml使用它的话。 If path is in css, then you need to put relative path to your pictures. 如果path在css中,那么您需要为图片添加相对路径。

It looks like your web site is configured to use SSL. 您的网站似乎配置为使用SSL。 Except that when running locally you probably do not have a valid SSL certificate which is trusted by a certification authority and the client browser is refusing to download the static resources. 除了在本地运行时,您可能没有证书颁发机构信任的有效SSL证书,并且客户端浏览器拒绝下载静态资源。 One way to fix that is to add the address as trusted. 解决此问题的一种方法是将地址添加为受信任的地址。 So copy paste the url to some of your CSS files in your browser address bar: 因此,请将网址粘贴到浏览器地址栏中的某些CSS文件中:

https://localhost/MyApp/Content/Main.css

and you will see a warning about the invalid certificate that you could ignore by adding an exception to your browser. 并且您将看到有关无效证书的警告,您可以通过向浏览器添加例外来忽略该证书。 Hit Ctrl+F5 to force a refresh once the exception is added and your application should start working properly. 添加异常后, 按Ctrl + F5强制刷新,应用程序应该开始正常工作。

When I had this problem I found that all of my CSS and Script files were encountering a HTTP 500 error when they were being downloaded in the browser (Firefox 33.0.2). 当我遇到这个问题时,我发现我的所有CSS和Script文件在浏览器中下载时都遇到了HTTP 500错误(Firefox 33.0.2)。

Killing the instance of my browser in the Task Manager and then starting afresh fixed the issue for me. 在任务管理器中杀死我的浏览器实例然后重新开始为我解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM