I need to map the path of a folder in my ASP MVC project from within another project in the solution. My model is in another class library. I have referenced System.web but I cannot seem to find Server.MapPath. <– Im not certain this is the way to do this in MVC
So my question is: What namespaces need to be included in my model’s project and how does one map to folder in the web application in MVC?
Thanks a bunch!
The type you’re referring to is HttpServerUtilityBase (typically called via Server.MapPath), but HttpRequestBase also has a MapPath method (typically called through Request.MapPath). These are part of ASP.NET 4’s System.Web.dll. Note that the concrete implementations of these are HttpServerUtilityWrapper and HttpRequestWrapper. But in general, you would never create instances of those. Instead, use instances that you obtain from the ControllerContext.
The question though is why does your model project need to call this? You might want to avoid having your model have a dependency on these types. Ideally you’d just pass the value in when you create your model. Another approach is to create your own lightweight interface for locating file paths and have your model depend on that.
But not knowing why you need it, I can only speculate. 🙂 If you have a good reason to use these classes, you’ll need to pass in an instance of one or the other into your model class so it can call it.