Mark Tabor

Mark Tabor

  • NA
  • 492
  • 39k

MVC Partial views changes the reference of css,Images and js

Oct 2 2016 7:02 AM
I have  a website in mvc and in _Layout.cshtml i have a slider my code of slider is shown below i am getting the images from a folder 
public class slider : Controller
{
public string src { get; set; }
public string title { get; set; }
public List<slider> GetData()
{
string[] filePaths = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("/Images/img/"));
//string[] filePaths = Directory.GetFiles(Url.Content(System.Web.HttpContext.Current.Request.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;
}
}
Now this slider works fine for Home Controller Index View , It also works fine for Service Controller Index view , but when i click on any of the service to show its details in a new partial view then my images get broken , when i inspect the element i found that instead of finding the image from the Root/Images/Go.png it is finding from Root/Service/Images/go.png where service is my controller method , Why it is so check the above code as well where i am using server.Mappath  

Answers (6)