Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Hi,

I deployed web application in my local iis under Default website.
The url is http://www.test.com/SubApplication/Controller/ActionResult
When i am giving some image path in html "/images/abc.jpg".I create the url "http://www.test.com/images/abc.jpg" but it should create "http://www.test.com/SubApplication/images/abc.jpg".Is there any setting in IIS for this.I googled but nothing found.


What I have tried:

I tried to change some settings in iis and also tried to change image path "~/Images/abc.jpg" and also tried to change routing but nothing works
Posted
Updated 15-Jul-16 0:01am

Use @Url.Content

Razor
<img src="@Url.Content("~/images/abc.jpg")"/>
 
Share this answer
 
Comments
vickyc049 15-Jul-16 5:55am    
for cshtml its fine.but i want to give path in javascript file
F-ES Sitecore 15-Jul-16 6:11am    
I'd try and use relative paths, but if you can't use relative paths you'll need to define the path to the image in a js variable on the view;

<script>
var myImgPath = '@Url.Content( ... )';
</script>

then in your js file you use that variable;

$("#myimg").attr("src", myImgPath);
The solution suggested by F-ES Sitecore will work for you. I am just adding some more information for your understanding.
/images/abc.jpg
A slash (/) in the start indicates a path from the root of a website. Since your website is www.test.com, /images will return this path - www.test.com/images.
There is no problem if we host our application under a website (usually done in production environment). But in local system we usually point our website to an application under default website as we do not need to create any domain host entries then. In such cases, relative paths creates issues. Tilde (~) comes to our rescue. In ASP.NET using Page.ResolveUrl to a path starting with ~ would give us path specific to our application. In MVC we can do the same using Url.Content. Hope that gives you some clarification.
 
Share this answer
 
Comments
vickyc049 15-Jul-16 6:09am    
Hi, Thnaks for ur solution.but one more image is there that image comes from css file.using background-image property. ~ not working for any css or javascript file.i already tried.actually ~ work with server side
Ankur\m/ 15-Jul-16 6:31am    
You are right, ~ cannot be used in CSS files. You can specify a relative path in your CSS files. For example say, your CSS files are inside a CSS folder placed at same location as images folder, you can give path as ../images/abc.jpg in your css file to refer the images inside images folder.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900