Using Server.MapPath directly in a class can cause errors, due to the inability of the class to directly use System.Web.UI.Page's non-static functions. There are two ways to resolve this:
Method One, add inheritance for class
Class CFoo:System.Web.UI.Page
Method Two, using the context to directly use
System.Web.HttpContext.Current.Server.MapPath
When using the method for a moment note: In C #, derived classes can only inherit from one class .
In method Two, System.Web.HttpContext.Current system.web is a namespace, HttpContext.Current is a class, HttpContext encapsulates all HTTP-specific information about an individual HTTP request, Current represents the HTTP request.
If we introduce a namespace system.web, we can omit it as HttpContext.Current.Server.MapPath.
In fact, this is not limited to Server.MapPath, but you can also use other properties and methods of the Server class , such as: Server.HTMLEncode (note case).
ASP. NET-how to use Server.MapPath in a class