ASP. NET-how to use Server.MapPath in a class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.