Alternative to Server.MapPath in a class file

by Joe Payne 10. January 2009 13:49

The Server.MapPath function a very useful, but it requires an HTTP Context or a Null exception error is thrown.

In situations where you don't have an HTTP context like a property in a class file, a slightly different approach is required.  I had to do some digging but I found this and it worked perfectly for me.  Let's say you have a String property on a class and you want a separate READONLY property to represent the full Server.MapPath equivalent.  Instead of using Server.MapPath("~/"+Me.OutFile), just use this:

System.Web.Hosting.HostingEnvironment.MapPath("~\" + Me.OutFile)

Tags: , ,

Personal

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Month List