Please disable your adblock and script blockers to view this page
 

Get Title,Description and Keywords Meta tags from URL in asp.net using C#, VB.Net

Get Title, Description and Keywords Meta tags from URL in asp.net
Click on image to enlarge

In this article I am going to explain with example How to get/read/fetch/retrieve Meta Tags e.g. Title,Description and Keywords from the Web URL or page URL.

Implementation: Just follow the following steps:

Step 1: First you need to add a namespace HtmlAgilityPack in your website project. You can download this from the link http://htmlagilitypack.codeplex.com.

Step 2: Just open the link in browser and click on the purple download button. It will download the zipped folder HtmlAgilityPack.1.4.6( Note: Version may be changed).

Step 3: Extract the folder.  You will see a list of folders there. Choose appropriate folder e.g. if you are using .Net framework 4.0 i.e. Visual studio 2010 then open the folder Net40 and copy the HtmlAgilityPack.dll file from it and paste in the Bin folder of your website in solution explorer. If there is no Bin folder there, then right click on the website name in solution explorer and click on Add ->Add ASP.NET folder -> select Bin and paste the HtmlAgilityPack.dll file in this folder.

Similarly for .Net framework 4.5 i.e. Visual studio 2012 open the folder Net45 and follow the same procedure as mention for Net framework 4.0

Step 4: In the design page (.aspx) place 4 Textbox controls and a Button control. Also place RequiredFieldValidator validation control to force the user to enter the URL in the Enter URL TextBox and RegularExpressionValidator validation control on the page to force user to enter the URL in the correct format e.g. http://www.webcodeexpert.com/
Source Code:

<fieldset style="width:450px;">
            <legend>Get Title, Description and Keywords Meta tags from URL in asp.net</legend>
            <table>
                <tr>
                    <td>Enter URL: </td>          
                          <td><asp:TextBox ID="txtUrl" runat="server" Width="250px"></asp:TextBox>
                        <asp:Button ID="btnFetchMetaTags" runat="server"  Text="Fetch MetaTags" OnClick="btnFetchMetaTags_Click" /><br />
                              <asp:RequiredFieldValidator ID="rfvWebUrl" runat="server" ControlToValidate="txtUrl" ErrorMessage="URL can not be left blank" SetFocusOnError="true"></asp:RequiredFieldValidator>
                              <asp:RegularExpressionValidator ID="rgeWebUrl" runat="server" ErrorMessage="Enter the URL in the valid format e.g. http://www.webcodeexpert.com/" ControlToValidate="txtUrl" ForeColor="#FF3300" SetFocusOnError="True" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?" Display="Dynamic"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>Title: </td>
                    <td><asp:TextBox ID="txtTitle" runat="server" Height="41px" TextMode="MultiLine" Width="380px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Description: </td>
                    <td><asp:TextBox ID="txtDesc" runat="server" Height="95px" TextMode="MultiLine" Width="380px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Keywords: </td>
                    <td><asp:TextBox ID="txtKeywords" runat="server" Height="95px" TextMode="MultiLine" Width="380px"></asp:TextBox></td>
                </tr>
            </table>
        </fieldset>

Step 5: Now we need to write the following code to get the meta tag information

C#.Net Code to get Meta Description of URL

Include the following name spaces:

using System.Net;
 using System.Text.RegularExpressions;
using HtmlAgilityPack;

and write the code on click event of Fetch MetaTags button as:

protected void btnFetchMetaTags_Click(object sender, EventArgs e)
    {
        String strUrl = txtUrl.Text;
        try
        {
            //To Get Title
            WebClient x = new WebClient();
            string pageSource = x.DownloadString(strUrl);
            txtTitle.Text = Regex.Match(pageSource, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
            //Creating a method to get Meta Tags like description and keyword meta tags
            GetMetaTagsDetails(strUrl);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }    
    }
    private void GetMetaTagsDetails(string strUrl)
    {
        //Get Meta Tags
        var webGet = new HtmlWeb();
        var document = webGet.Load(strUrl);
        var metaTags = document.DocumentNode.SelectNodes("//meta");
        if (metaTags != null)
        {
            foreach (var tag in metaTags)
            {
                if (tag.Attributes["name"] != null && tag.Attributes["content"] != null && tag.Attributes["name"].Value == "description")
                {
                    txtDesc.Text = tag.Attributes["content"].Value;
                }

                if (tag.Attributes["name"] != null && tag.Attributes["content"] != null && tag.Attributes["name"].Value == "keywords")
                {
                    txtKeywords.Text = tag.Attributes["content"].Value;
                }
            }
        }
    }   

VB.Net Code to get Meta Description of URL

Import the following name spaces:

Imports System.Net
Imports System.Text.RegularExpressions
Imports HtmlAgilityPack

and write the code on click event of Fetch MetaTags button as:

Protected Sub btnFetchMetaTags_Click(sender As Object, e As EventArgs)
        Dim strUrl As [String] = txtUrl.Text
        Try
            'To Get Title
            Dim x As New WebClient()
            Dim pageSource As String = x.DownloadString(strUrl)
            txtTitle.Text = Regex.Match(pageSource, "\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups("Title").Value
            'Creating a method to get Meta Tags like description and keyword meta tags
            GetMetaTagsDetails(strUrl)
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
        End Try
    End Sub
    Private Sub GetMetaTagsDetails(strUrl As String)
        'Get Meta Tags
        Dim webGet = New HtmlWeb()
        Dim document = webGet.Load(strUrl)
        Dim metaTags = document.DocumentNode.SelectNodes("//meta")
        If metaTags IsNot Nothing Then
            For Each tag In metaTags
                If tag.Attributes("name") IsNot Nothing AndAlso tag.Attributes("content") IsNot Nothing AndAlso tag.Attributes("name").Value = "description" Then
                    txtDesc.Text = tag.Attributes("content").Value
                End If

                If tag.Attributes("name") IsNot Nothing AndAlso tag.Attributes("content") IsNot Nothing AndAlso tag.Attributes("name").Value = "keywords" Then
                    txtKeywords.Text = tag.Attributes("content").Value
                End If
            Next
        End If
    End Sub

Step 6: Build the solutions and run the website. You will get Title, Description and Keywords Meta tags of the entered URL.

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

8 comments

Click here for comments
Anonymous
admin
October 05, 2013 ×

thanks its really a useful article ....keep uploading

Reply
avatar
October 05, 2013 ×

i am glad you liked my article..stay connected and keep reading :)

Reply
avatar
Rohit Kumar
admin
October 16, 2013 ×

Thank your sir....

Reply
avatar
October 16, 2013 ×

Your welcome Rohit..stay connected and keep reading new updates..:)

Reply
avatar
October 29, 2013 ×

Using this code i m getting this error
"The type or namespace name 'HtmlAgilityPack' could not be found"
how to fix this ?

Reply
avatar
October 30, 2013 ×

Hi, i suggest you to read the steps mentioned in the article carefully..and you need to add a namespace HtmlAgilityPack in your website project. You can download this from the link http://htmlagilitypack.codeplex.com.

Reply
avatar
Anonymous
admin
January 02, 2014 ×

your code is really good and working fine,
My question is how can i find image

Reply
avatar
January 02, 2014 ×

Hello, I will also add the functionality to get the image also..so stay connected and keep reading..:)

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon