How to add namespace to aspx file?

I added c # code to aspx file but it shows error

The type or name of the Mail namespace does not exist in the class or namespace 'System.Net' (do you miss the assembly reference?)

How can I add nampespace to an aspx file, I tried <%@ import namespace="Westwind.Tools"%> , but it does not work?

+57
Mar 03 '10 at 8:18
source share
3 answers
 <%@ Import Namespace="System.Net.Mail" %> 
+95
Mar 03 '10 at 8:19
source share

I assume this is on a website and that the page has no code?

 <%@ Page Language="C#" %> <%@ Import Namespace="System.Net.Mail"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { System.Net.Mail.SmtpClient client = new SmtpClient(); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> </body> </html> 

It seems to work for me.

If it's not on the website and / or it has code, why do you need to reference the namespace in the aspx file?

Hope this helps

+7
Mar 03 '10 at 8:39
source share

To add a namespace globally, not page by page, just put the namespace in your web.config file.

 <configuration> <system.web> <pages> <namespaces> <add namespace="Your.Namespace"/> </namespaces> </pages> </system.web> </configuration> 

You may need to restart Visual Studio for IntelliSense to turn on.

You can also create a mini web.config file in a directory to import the namespace only into ASPX files in that directory and subdirectories, and not apply it globally.

0
Jul 09 '19 at 15:24
source share



All Articles