The type or namespace name ' Server ' was not found (is a using directive or an assembly reference missing?) ) _ Self-Study Process

Source: Internet
Author: User
Tags httpcontext
Today will webserice inside of a code, copy to a C # class, the result of running a compilation error.
DataBase = Server.MapPath ("Data.mdb");
Error: Cannot find type or namespace name ' Server ' (using directive or assembly reference is missing?) )

But I did cite the name space using System.Web; why?
Because the full path to the Server is System.Web.HttpContext.Current.Server. Using system.web is clearly not sufficient, but it is possible to write Server.MapPath "Data.mdb" directly in classes that inherit from the built-in page class, because the page class already contains these classpath.

The solution to this type of error is summarized as follows
1, ensure the correct assembly reference:
Make sure that the assembly that contains the namespace is referenced. If you are developing in Visual Studio. NET, you can go to the Project menu and click Add Reference. On the. NET tab, select the assembly that contains the namespace that you want to import, or browse to the folder that contains the assembly. If you are using the command-line compiler, add the corresponding switch (/reference) to the compilation statement.
2, the right namespace:
(1)* If you execute this statement from a class that inherits from the page class, you can simply use the
DataBase = Server.MapPath ("Data.mdb");
(2) If in the ordinary class
A the using statement is placed at the top of the code file containing the type reference, ensuring that the namespace is imported into the project:
Using System.Web;
DataBase = HttpContext.Current.Server.MapPath ("Data.mdb");
B If there is no using statement, the reference to the type is fully qualified:
DataBase = System.Web.HttpContext.Current.Server.MapPath ("Data.mdb");

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.