This tutorial will show you how to perform a DNS query using the .NET System.Net class, ASP.NET 2.0 and C#.NET

The .NET Framework offers a number of types that makes accessing networked resources easy.
To perform a DNS query, we will need to first import the System.Net namespace. The System.Net namespace contains the Dns.GetHostByName method, IPHostEntry container class and IPAddress class we will use to perform our DNS query.

We’ll put our code in a subroutine called DNSLookup() and call it from the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it calls our DNSLookup() function. This function creates a new instance of the IPHostEntry container class and assigns it a host entry returned from the Dns.GetHostByName method. It then create an array of IPAddress objects using the AddressList of our host entry object. After that, it iterates through each ip and adds it to the text box.

We have two textboxes, a submit button, and a label on the front end for user interaction. The front end .aspx page looks something like this:

The flow for the code behind page is as follows:

Download Source Files