Introduction
This article explains how to get "title and meta description" of a URL using ASP.Net. To do that just use the following procedure.
Step 1
Download "Html Agility Pack" from http://html-agility-pack.net/?z=codeplex
I will explain how to use that downloaded file later in this article.
Step 2
Make the design to get the "title and meta description" of a URL.
Code for the design
Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>Get Title and Descripition of Live URl</title>
  6. <style type="text/css">
  7. .auto-style1 {
  8. width: 100%;
  9. }
  10. .auto-style2 {
  11. height: 15px;
  12. }
  13. .auto-style3 {
  14. height: 15px;
  15. width: 172px;
  16. }
  17. .auto-style4 {
  18. width: 172px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <form id="form1" runat="server">
  24. <div>
  25. <h2>Get Title and Descripition of Live URl</h2>
  26. </div>
  27. <table style="border:groove #00FF99">
  28. <tr>
  29. <td class="auto-style3"><h2 style="width: 167px; height: 20px">Enter Url</h2></td>
  30. <td class="auto-style2">
  31. <asp:TextBox ID="TextBox1" runat="server" Width="530px" style="margin-left: 0px" Height="28px"></asp:TextBox>
  32. </td>
  33. </tr>
  34. <tr>
  35. <td class="auto-style4"><h2>Title</h2></td>
  36. <td>
  37. <asp:TextBox ID="TextBox2" runat="server" Width="528px" Height="29px" Font-Bold="True" Font-Names="Times New Roman" Font-Size="Medium"></asp:TextBox>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td class="auto-style4"><h2>Descripition</h2></td>
  42. <td>
  43. <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Width="531px" Height="90px" Font-Bold="True" Font-Names="Times New Roman" Font-Size="Medium"></asp:TextBox>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td colspan="2">
  48. <br />
  49. <asp:Button ID="Button1" runat="server" Text="Get" BackColor="#CC6600" ForeColor="#FFFF99" Height="33px" Width="85px" OnClick="Button1_Click" />
  50. </td>
  51. </tr>
  52. </table>
  53. </form>
  54. </body>
  55. </html>
For the code above, the output looks like:
Step 3
Add the reference of the preceding download "HtmlAgilitypack".
Now let's move to the "Default.aspx.cs" code.
In this you require the following namespaces:
  1. using System;
  2. using System.Net;
  3. using System.Text.RegularExpressions;
  4. using HtmlAgilityPack;
The last namespace is available to you, when you add the reference of the above download "HtmlAgilitypack". Now let's concern ourselves with the C# code
The code given below enables you to retrieve the title and meta description of a live "URL" in ASP.Net.
  1. using System;
  2. using System.Net;
  3. using System.Text.RegularExpressions; // Provides access to regular expression related api.
  4. using HtmlAgilityPack;
  5. public partial class _Default : System.Web.UI.Page
  6. {
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. }
  10. protected void Button1_Click(object sender, EventArgs e)
  11. {
  12. String url = TextBox1.Text;
  13. WebClient x = new WebClient();
  14. string sourcedata = x.DownloadString(url);
  15. TextBox2.Text = Regex.Match(sourcedata, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
  16. GetMetaTagValue(url);
  17. }
  18. private void GetMetaTagValue(string url)
  19. {
  20. var getHtmlDoc = new HtmlWeb();
  21. var document = getHtmlDoc.Load(url);
  22. var metaTags = document.DocumentNode.SelectNodes("//meta");
  23. if (metaTags != null)
  24. {
  25. foreach (var sitetag in metaTags)
  26. {
  27. if (sitetag.Attributes["name"] != null && sitetag.Attributes["content"] != null && sitetag.Attributes["name"].Value == "description")
  28. {
  29. TextBox3.Text = sitetag.Attributes["content"].Value;
  30. }
  31. }
  32. }
  33. }
  34. }
When you write the live "URL" in the "Enter URL" corresponding TextBox then click on the button the output looks like: