简体   繁体   English

Visual Studio中样式表的相对路径无法在预览中工作

[英]Relative path to a stylesheet in Visual studio not working in preview

I'm assuming this is an easy question, but I'll be darned if I can find the answer. 我假设这是一个简单的问题,但如果我能找到答案,我会很高兴。

I have a website in Visual Studio 2008. The paths to the stylesheets (and images) are in the following format /css/stylesheetname.css 我在Visual Studio 2008中有一个网站。样式表(和图像)的路径采用以下格式/css/stylesheetname.css

At the root of the web project in Visual studio the folder exists as does the stylesheet. 在Visual Studio中Web项目的根目录下,该文件夹与样式表一样存在。 These paths work fine when running it in IIS. 在IIS中运行它们时,这些路径可以正常工作。

If I use the inbuilt webserver in Visual Studio the paths fail because it puts the projectname in the path ie http://localhost:2020/projectname/default.aspx 如果我在Visual Studio中使用内置的Web服务器,则路径会失败,因为它将项目名放在路径中,即http:// localhost:2020 / projectname / default.aspx

In this case the / takes the path right back to http://localhost:2020 在这种情况下,/将路径直接返回到http:// localhost:2020

This is further compounded by the fact that if you click "design" the styles that import background images all fail although the stylesheet is imported correctly (becuase all other aspects of the stylesheet work ie .class{font-family:arial;} works but .class{background: url(/images/image.jpg)} does not). 如果单击“设计”,导入背景图像的样式都会失败,尽管样式表正确导入(因为样式表工作的所有其他方面,即.class{font-family:arial;}可以工作但是这一点更加复杂) .class{background: url(/images/image.jpg)}没有)。

I guess it's all to do with how Visual studio calculates its root path for the website, however I can't find a setting to change this. 我想这与Visual Studio如何计算网站的根路径有关,但我找不到改变它的设置。

Any ideas?? 有任何想法吗?? Update: as per Egil Hansen's answer I converted the paths in the CSS file to relative paths. 更新:根据Egil Hansen的回答,我将CSS文件中的路径转换为相对路径。 However the background images still do not display in Design mode. 但是,背景图像仍然无法在设计模式下显示。 I'll take a look at using Themes to get round this in due course. 我将在适当的时候看看使用Themes来解决这个问题。

I think the correct solution is to use relative urls in the style sheet instead of absolute urls as you use now. 我认为正确的解决方案是在样式表中使用相对URL而不是现在使用的绝对URL。

Do note that relative urls in style sheets are relative to the location of the style sheet, not the current page being view by the browser. 请注意,样式表中的相对URL相对于样式表的位置,而不是浏览器正在查看的当前页面。

If you use ASP.NET Themes, you can put all your website graphics in a /App_Themes/YourTheme/Images/ folder, and put your style sheet in the /App_Themes/YourTheme/ folder. 如果您使用ASP.NET主题,则可以将所有网站图形放在/App_Themes/YourTheme/Images/文件夹中,并将样式表放在/App_Themes/YourTheme/文件夹中。

In your style sheet, you can then simply reference an image with url(Images/img.gif) , and it will work both online and in development. 在你的样式表中,你可以简单地用url(Images/img.gif)引用一个图像,它可以在线和开发中使用。

The you just need to assign your ASP.NET Theme to the page(s) you want, either through web.config's Pages section ( <pages styleSheetTheme="Default"> ) that will assign a theme to all pages on the website or through the <%@ Page ... directive on each page. 您只需要将ASP.NET主题分配给您想要的页面,或者通过web.config的Pages部分( <pages styleSheetTheme="Default"> )将主题分配给网站上的所有页面或通过每页上的<%@ Page ...指令。

In general, you can do some really neat things with ASP.NET Themes and Skins, just take a look at the ASP.NET Themes and Skins Overview over at msdn.microsoft.com. 通常,您可以使用ASP.NET主题和皮肤做一些非常简洁的事情,只需在msdn.microsoft.com上查看ASP.NET主题和皮肤概述

There are a few issues to be aware of with Themes in ASP.NET, take a look at my post How to take control of style sheets in ASP.NET Themes with the StylePlaceHolder and Style control , which explains and solves the issues I have come across so far. 在ASP.NET中有一些需要注意的问题,请看看我的帖子如何使用StylePlaceHolder和Style控件控制ASP.NET主题中的样式表 ,这解释并解决了我遇到的问题到目前为止。

not sure if this works for VS 2008 or not, but im using visual web developer 2010 and it worked for me: 不确定这是否适用于VS 2008,但我使用可视化Web开发人员2010,它对我有用:

1) click on the project in the solution explorer 1)单击解决方案资源管理器中的项目

2) it shows a "Virtual Path" property which is defaulted to "/projectname" 2)它显示“虚拟路径”属性,默认为“/ projectname”

3) change it to "/" instead and it seems to do what is desired 3)改为将其改为“/”,似乎做了所需的事情

let me know if this works for you! 如果这对你有用,请告诉我!

I have been running projects using the custom Image folder for all my graphics for ASP.Net applications. 我一直在使用自定义Image文件夹为ASP.Net应用程序的所有图形运行项目。 While there have been advancement in this regard with the App_Theme and App_Code folder(s) available in the progressive VS IDE; 虽然在渐进式VS IDE中可用的App_Theme和App_Code文件夹在这方面取得了进步; I still kept my folder and it has not disapponited when deploying it on the server. 我仍保留我的文件夹,并且在服务器上部署它时没有失望。

So with that said - the proverbial folder will be sitting with all the bin, App_Code and _Themes and the reference to it is made through this way 所以说 - 谚语文件夹将与所有bin,App_Code和_Themes坐在一起,并通过这种方式对它进行引用

background: url(../image/..); background:url(../ image / ..);

of course the code above sitting in the CSS file. 当然上面的代码坐在CSS文件中。 It works for me all the time 它一直适用于我

I had set path css url image by code { background:url(/images/xxx.jpg) no-repeat; 我通过代码{background:url(/images/xxx.jpg)no-repeat设置了路径css url图像; } and running file at IIS, so must to point default website to your project 并在IIS上运行文件,因此必须将默认网站指向您的项目

how to running testing preview type: http://localhost/default.aspx 如何运行测试预览类型: http://localhost/default.aspx

this is correct path same running on server include file js or css can use "/" root path 这是正确的路径,在服务器上运行包括文件js或css可以使用“/”根路径

cheers Noboyband 欢呼Noboyband

it has been ages since I did anything in css, but maybe url(./images/image.jpg) will work? 自从我在CSS中做了什么以来已经很久了,但也许url(./ images / image.jpg)会起作用吗?

Edit: Or rather ~/format /css/stylesheetname.css or ./format /css/stylesheetname.css as the url to the stylesheet. 编辑:或者更确切地说〜/ format /css/stylesheetname.css或./format /css/stylesheetname.css作为样式表的url。

I had the same issue and it drove me crazy. 我有同样的问题,它让我发疯。 Solution is to add an Apps_Theme folder and copy the images into there. 解决方案是添加Apps_Theme文件夹并将图像复制到那里。 When you publish the site the folder structure is preserved and the imnges display. 发布站点时,将保留文件夹结构并显示imnges。

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

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