1

I have a class in my Class Library that's doing all sorts of validations and consistency checks for files before returning the result to the Web, and it used to work fine in WebForms with:

System.Web.HttpContext.Current.Server.MapPath("myFilePath here");

But now that I'm doing the same with MVC, the routing is messing up the MapPath. How can I get the "base" path of the application in the Class Library using MvC?

Danicco
  • 1,573
  • 2
  • 23
  • 49
  • There is another thread that talks about it: http://stackoverflow.com/questions/7600118/using-server-mappath-in-mvc3 –  Jun 27 '13 at 20:32

2 Answers2

2

Use:

HttpContext.Server.MapPath("~/myFilePath here");
Ani
  • 4,473
  • 4
  • 26
  • 31
0

I normally end up passing that path from controller to the helper library where it is needed.

Other option is using:

System.Web.Hosting.HostingEnvironment.MapPath("~/Your relative path from root website")

BTW, to use HostingEnvironment.MapPath(), you will need to add reference to System.Web.

Romias
  • 13,783
  • 7
  • 56
  • 85