How to add namespace in aspx file?

asp.net

asp.net Problem Overview


i have added C# code in aspx file, but it is showing error

> The type or namespace name 'Mail' does not exist in the class or > namespace 'System.Net' (are you missing an assembly reference?)

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

asp.net Solutions


Solution 1 - asp.net

<%@ Import Namespace="System.Net.Mail" %>

Solution 2 - asp.net

I'm assuming that it's in a website and that the page doesn't have code behind?

<%@ 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>

This seems to work for me.

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

Hope this helps

Solution 3 - asp.net

To add the namespace globally instead of page-by-page, just put the namespace in your web.config.

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

You might have to restart Visual Studio for the IntelliSense to kick in.

You can also create a mini web.config in a directory to only import the namespace into ASPX files within that directory and sub-directories instead of applying it globally.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheeryView Question on Stackoverflow
Solution 1 - asp.netDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.netWestDiscGolfView Answer on Stackoverflow
Solution 3 - asp.netJacob StammView Answer on Stackoverflow