Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple mvc web application , in which i have a menu like
home,services,contactus when i click the service menu the new service controller invokes and service Index view opened, and this view have service details and also a read more button , when i click the read more button I want to show the more details like description of that service, all these things are working properly , The issue is that when i click the Readmore linkbutton on service view It loses the Path of my JS and Css folder , so my _Layout.cshtml having the slider and it did not show the images of that slider when i clicked the Read more button it shows the other details properly but the path of my Images and Css,JS get disturbed , I inspact the issue and check that

this is correct path but when i hover on img tag it show the path like that
http://localhost:60553/Service/Images/img/rsz_malibu-2.png Failed to load resource

I mean it is looking my CSS JS and images in Service Folder instead of root folder how to fix that

What I have tried:

I have check the method of my slider it is having correct path in code but when i inspect the element in browser it shows the below path
http://localhost:60553/Service/Images/img/rsz_malibu-2.png
It is finding the image in SErvice Folder instead of in root then Images/img/rsz_malibu-2.png
Posted
Updated 3-Oct-16 5:03am
Comments
Suvendu Shekhar Giri 1-Oct-16 14:53pm    
share the relevant code
Malikdanish 2-Oct-16 5:21am    
Basically My silder and css works fine on home controller also on service controller but when i click on any of the service details then the images get broken i found why it is so when i hover on the img tag using inspect element in chrome browser it shows that instead of finding the image from root/Images/google.png it is finding the image from root/Service/images/google.png
below is my readmore button as well as controller method
@Html.ActionLink("ReadMore", "Details", "Service", new { id = item.ID },null)
public ActionResult Details(int id)
{
slider sd = new slider();
//sd.GetData();

// viewModelVm vm = new viewModelVm();


Service sr = new Service();
sr.Getdatabyid(id);
viewModelVm vm = new viewModelVm();
vm.slider = sd.GetData();
vm.service = sr.Getdatabyid(id);
Service ss = sr.Getdatabyid(id);
return View(vm);
}

Malikdanish 2-Oct-16 5:24am    
public List(slider) GetData()
{
string[] filePaths = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("/Images/img/"));
List(slider) files = new List(slider)();
var model = new System.Collections.Generic.List(slider)();
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
model.Add(new slider
{
title = fileName.Split('.')[0].ToString(),
src = "../Images/img/" + fileName
});
}
return model;
}
angle brakets are now allowed so i place () brakets

1 solution

I think your issue is in your GetData method, specifically here:

C#
src = "../Images/img/" + fileName



I think you need to change that to something like

C#
src = HttpContext.Current.Server.MapPath("~/Images/img/" + fileName);


The way you are currently doing it, the call is relative to your request. Using Server.MapPath maps your URL to the image relative to the server.
 
Share this answer
 
Comments
Malikdanish 3-Oct-16 12:31pm    
Dear thanks for your response by doing that my images from the home{index view} also disappear when i checked the path of the images it is like that
C:\Users\dhabib\Documents\Visual Studio 2010\Projects\MVC\BusinessApplicationTemplate\BusinessApplicationTemplate\Images\img\009-Living_Room-2169443-large-2.png
I have solve the issue by
src = "../../Images/img/" + fileName
Thanks again for your support
David_Wimbley 3-Oct-16 13:17pm    
Well ../../Images won't solve your problem, you'll end up nesting the deeper and deeper you go.

Try
Url.Content("~/Images/img/" + fileName);
instead. I forget the exact one but i think Url.Content should work.
Malikdanish 3-Oct-16 13:46pm    
No Url.Content is available i thing Server.MapPath() is an alternative but it also not worked for me

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