开发者

Relative urls for images and js files in MVC application - diff behaviour on local and production server

I have an MVC web application, the urls like following in my views\ folder:

<img src="../../Images/Delete.png"/>

are working on my localhost, but when I deployed the application on production server, they stopped working and when I use single ".." instead of double "../.." , they start working on production server.

Why is this happening 开发者_运维问答?


This is happening because you probably have a virtual directory in production server and not on localhost. So always use URL helpers when dealing with urls and never hardcode them as you did in your example:

<img src="<%= Url.Content("~/Images/Delete.png") %>" />

You could use relative paths only inside CSS files (for background images for example). Inside CSS files paths are relative to the location of the CSS, but the inclusion of the CSS itself should be done using helpers of course:

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


The relative path to the images directory must be different on the production server.

You need to make sure that on your development server the directory layout is the same as on the production server.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜