1

I have the following method code:

private Image GetMapIcon()
{
  //TODO Fix path
  return Image.FromFile("D:\\Work\\FindandCompare\\Main\\NHSChoices.FindAndCompare.Web\\Content\\img\\mapIcon.png");
}

As you can see the path will only work on my machine. I tried all sorts to get it to work without being so specific, but I couldn't! For example:

private Image GetMapIcon()
{
  //TODO Fix path
  return Image.FromFile(Url.Content("~/Content/img/mapIcon.png"));
}

The method is sitting within an MVC Controller.

I do not want to add a whole new Class Library project to the VS Solution to hold a resource. Is there an easier way that will work once this is deployed to the server?

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • This might be useful: http://stackoverflow.com/questions/1875368/best-practice-for-asp-net-mvc-resource-files – Ulric May 20 '16 at 10:52

1 Answers1

2

You can use Server.MapPath:

private Image GetMapIcon()
{
  return Server.MapPath("~/Content/img/mapIcon.png")
}

To include the image in the publish simply highlight the image in the solution explorer then change the 'Build action' to 'Content'.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104