Click on image to enlarge |
Introduction: In previous articles i explained How to get current page URL/Address in asp.net and How to Count and display remaining characters in the multiline textbox in asp.net and How to show Validation guidelines in web forms using JavaScript in Asp.net and How to implement JavaScript validation in asp.net website and Convert Rupees,Currency or Numbers to Words and Get age in years,months,days,hours and seconds from DOB and WCF Service to bind,insert,edit,update,delete from sql server database in asp.net and How to Validate and upload image files in asp.net.
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 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/
- If you don’t know much about RequiredFieldValidator validation control then read my article How to use RequiredFieldValidator validation control with example in asp.net
- If you don’t know much about RegularExpressionValidator validation control then read my article How to use RegularExpressionValidator validation control with example in asp.net
<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-
./?%&=]*)?" 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."
8 comments
Click here for commentsthanks its really a useful article ....keep uploading
Replyi am glad you liked my article..stay connected and keep reading :)
ReplyThank your sir....
ReplyYour welcome Rohit..stay connected and keep reading new updates..:)
ReplyUsing this code i m getting this error
Reply"The type or namespace name 'HtmlAgilityPack' could not be found"
how to fix this ?
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.
Replyyour code is really good and working fine,
ReplyMy question is how can i find image
Hello, I will also add the functionality to get the image also..so stay connected and keep reading..:)
ReplyIf 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..