开发者

ASP.NET MVC2: how to set paths for the image links continaing with in the CSS file

I our application we have image patches set in CSS file as shown below

.HeaderShodow {
    background:url('../../App_Images/HeaderShodow.gif') repeat-x top left;
    height:5px;
}

when we move this application to iss6 server the images or not rendered

we have sent css/image/script lin开发者_C百科ks with in the aspx file as below

<link href="<%= Url.Content("~/App_Themes/Common.css")%>" rel="stylesheet" type="text/css" />

but how to set when the image links are with in the css file

Thanks


The reason this doen't work is because there's the virtual directory name appended in IIS. To avoid this problem make sure you always include your CSS file using proper helpers and do not hardcode the location:

<!-- Notice the ~ in the href attribute which points to the root of the application -->
<link rel="stylesheet" href="<%= Url.Content("~/styles/somestyle.css") %>" type="text/css" />

then in the css file make sure that image paths are relative to the location of this CSS file.


My recommendation, as CSS image URL's are relative to the CSS file itself, is to have an images folder in the same directory as the CSS file itself.

so your directory would look like:

/Content/site.css
/Content/Images/blah.png

that means in your CSS file you can reference the images like:

.myClass
{
    background-image: url('Images/blah.png');
}

and if in your HTML you reference the CSS with the Url.Content() code, you will always get the correct path.

<link href="<%: Url.Content("~/Content/site.css") %>" rel="stylesheet" type="text/css" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜