Introduction
Here I will explain how to get title & meta
description of url in asp.net
using C# and VB.NET
or get page title and description from page url in asp.net
using HTMLAgilityPack in C# and VB.NET.
Description
In previous articles I explained jQuery Get Current Page url & title, Get url parameter values using jQuery, jQuery Dropdown menu with CSS, Asp.net Interview questions, Send values from one page to another page using QueryString,
Highlight Gridview records based on search and many
articles relating to Gridview,
SQL, jQuery,asp.net,
C#,VB.NET.
Now I will explain how to get title & description of url in asp.net
using C# and VB.NET.
To get title & description of
url we need to write the following code in aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get
Title and Meta Description from Live URL</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<h3>Get
Title and Meta Description from Live URL</h3>
<table>
<tr>
<td>
<b>Enter
Url:</b>
</td>
<td>
<asp:TextBox ID="txtURL" runat="server" AutoPostBack="True" ontextchanged="txtURL_TextChanged" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<asp:TextBox ID="txtTiltle" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top">
<b>Description:</b>
</td>
<td>
<asp:TextBox ID="txtDesc" runat="server" Rows="7" Columns="40" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
Now in code behind add the following namespaces
C#
Code
using System;
using System.Net;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
|
Here I added new HtmlAgilityPack namespace by using this
reference we can parse out HTML pages you can get this dll reference from
attached sample folder or from this url http://htmlagilitypack.codeplex.com
Now add below code in code
behind
protected void
txtURL_TextChanged(object sender, EventArgs e)
{
String url = txtURL.Text;
//Get Title
WebClient x = new WebClient();
string source = x.DownloadString(url);
txtTiltle.Text = Regex.Match(source,
@"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",
RegexOptions.IgnoreCase).Groups["Title"].Value;
//Method to get Meta Tags
GetMetaTagValues(url);
}
private void
GetMetaTagValues(string url)
{
//Get Meta Tags
var webGet = new HtmlWeb();
var document = webGet.Load(url);
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;
}
}
}
}
|
VB.NET
Code
Imports System.Net
Imports System.Text.RegularExpressions
Imports HtmlAgilityPack
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub
Page_Load(sender As Object,
e As EventArgs)
End Sub
Protected Sub
txtURL_TextChanged(sender As Object, e As EventArgs)
Dim url As [String] = txtURL.Text
'Get Title
Dim x As New WebClient()
Dim source As String = x.DownloadString(url)
txtTiltle.Text = Regex.Match(source,
"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",
RegexOptions.IgnoreCase).Groups("Title").Value
'Method to get Meta Tags
GetMetaTagValues(url)
End Sub
Private Sub
GetMetaTagValues(url As String)
'Get Meta Tags
Dim webGet = New HtmlWeb()
Dim document = webGet.Load(url)
Dim metaTags = document.DocumentNode.SelectNodes("//meta")
Dim tag
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
Next
End If
End Sub
End Class
|
Demo
Download Sample Code Attached
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
d
System.IO.FileNotFoundException: Could not find 1. file 'C:\WINDOWS\system32\www.facebook.com'.
and
2. System.Net.WebException: The remote name could not be resolved:
How to Resolve these errors....
Thanks
for this HtmlAgilityPack; namespace which files dll need to be added. i downloaded from codeplex it has so many folder are available. In which dll i need to include..
Thanks,
Ramanathan.N
Hii Sir. Suresh I am Dhiraj a Student of class XI.I want to make my own website in .net and i found your website as the best website on Internet for learners.
Sir, Can you post a new article, in that "a user can post his questions and at bottom a button to comment on that considering the user is logged in or not if not then login using fb."
This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself..
buy real fb likes
google not allowimg meta tags any more ! how to get description now ??
Note: Only a member of this blog may post a comment.