Using meta tags in asp.net 2.0 with a master page (extended @page directives)

Source: Internet
Author: User
Tags extend header visual studio backup
asp.net

Introduced
The motherboard page is a very powerful feature in ASP.net 2.0, but it does not provide a basic approach to search engine optimization. If you want your Web page to be indexed and enhanced by search engines, then you need to specify a title and meta tag on each page. This article explains how to extend your asp.net page so that you can specify the description of your meta tag and the keyword of the meta tag in the @page instruction of your content page when using the motherboard page.


Background
When you want to optimize your Web page for search engines, setting the title tag of the page and the meta description of the page is one of the most important factors. The <title> and meta tags are actually in the HTML

<title>
Rhinoback Professional Secure Online Backup Services for Small and Medium BUSINESS-SMB
</title>
<meta name= "description" content= "Professional Online Backup Services.
Rhinoback provides robust backup functionality at affordable prices.
Premium features, Premium services, low prices. Get the most for
Your with rhinoback! "/>
<meta name= "keywords" content= backup, online backup, secure backup, cheap backup,
Free Backup, offsite backup,internet backup, secure files, offsite data storage,
Privacy, security, features, low prices, premium service, remote backup "/>
<body>
<!--page Content-->
</body>


The text of the <title> label is displayed at the top of the browser. From the example below, you can see the <title> display in IE


When your page is indexed by a search engine, the meta description text is displayed in the search engine's list. The following example is from Google. The following text from the underlined title comes from the page's Meta description label. If there is no Meta description tag, your page will appear as a text on your page in the search engine list. It is much better to specify a descriptive text for each page than to give it to the search engine.

The motherboard page has proven to be a very useful feature in ASP.net 2.0. This article is not about the details of the master page or how to implement it, because there are so many articles. When we use

<%@ Page language= "C #" masterpagefile= "~/pagetags.master" autoeventwireup= "true" codefile= "Home.aspx.cs" Inherits = "Home" title= "My home page Title"%>


The @page instruction above shows that this is a content page that uses a motherboard page. If you want to specify a meta tag, you should specify it in the content page. You can see that there is a "Description" attribute in the @page directive, but it cannot create a meta description tag on your page. In fact, even if you specify the "Description" attribute, it will eventually be ignored without doing anything.

I couldn't make all the pages of the site use the same description, and I wanted to add a keyword to each page. The first solution we can think of is to insert the META tags we want into the C#

protected void Page_Load (object sender, EventArgs e)
{
Htmlmeta tag = new Htmlmeta ();
Tag. Name = "description";
Tag. Content = "My description to this page";
HEADER.CONTROLS.ADD (tag);
}


Vb

Sub Page_Load () Sub Page_Load (ByVal sender as Object, ByVal e as EventArgs)
Dim tag as Htmlmeta = New Htmlmeta ()
Tag. Name = "description"
Tag. Content = "My description to this page"
HEADER.CONTROLS.ADD (TAG)
End Sub


One problem with this workaround is that the title of the page, the meta description, and the META keyword are all interrelated, so we think it's best to have the title and description specified in the same file. It's really easy to add a <script> tag to the. aspx page in the Page_Load method, but I want a simpler setup and a solution for checking each page label.

In the next scenario we'll see how to add Meta tags to each page by extending the @page instruction.


Solution
I created a page base class that inherits from System.Web.UI.Page, and lets my content page inherit the BasePage class of my self. The BasePage class contains code that adds meta tags to the header control in an. aspx page, and when we inherit the BasePage, the code only needs to exist in one place, not every page is written.
C#

Using System;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Text.RegularExpressions;

/**////<SUMMARY>
To extend the base class to add meta tags to content pages
</SUMMARY>
public class Basepage:page
{
private string _keywords;
private string _description;
Constructors
Add an INIT event handler
Public BasePage ()
{
Init + = new EventHandler (basepage_init);
}

The page will use this base class to initialize the
Add meta keywords and meta description if available
void Basepage_init (object sender, EventArgs e)
{

if (! String.IsNullOrEmpty (Meta_keywords))
{
Htmlmeta tag = new Htmlmeta ();
Tag. Name = "keywords";
Tag. Content = Meta_keywords;
HEADER.CONTROLS.ADD (tag);
}

if (! String.IsNullOrEmpty (meta_description))
{
Htmlmeta tag = new Htmlmeta ();
Tag. Name = "description";
Tag. Content = meta_description;
HEADER.CONTROLS.ADD (tag);
}
}

/**////<SUMMARY>
Gets or sets the META keyword for the page
</SUMMARY>
public string Meta_keywords
{
Get
{
return _keywords;
}
Set
{
Remove Extra Space
Translator Note: \s matches any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
_keywords = Regex.Replace (value, "\\s+", "");
}
}

/**////<SUMMARY>
Gets or sets the meta description of the page
</SUMMARY>
public string Meta_description
{
Get
{
return _description;
}
Set
{
Remove Extra Space
Translator Note: \s matches any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
_description = Regex.Replace (value, "\\s+", "");
}
}
}


Vb

Imports System
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Text.RegularExpressions


' To extend the base class to add meta tags to content pages

Public Class Basepageclass BasePage
Inherits Page

Dim _keywords as String
Dim _description as String
' page will be initialized with this base class
' Add an init event handling
Public Sub New () Sub New ()
AddHandler Init, New EventHandler (AddressOf basepage_init)
End Sub

' page will be initialized with this base class
' Add meta keywords and meta description if available
Sub Basepage_init () Sub Basepage_init (ByVal sender as Object, ByVal e as EventArgs)

If not String.IsNullOrEmpty (meta_keywords) Then
Dim tag as Htmlmeta = New Htmlmeta ()
Tag. Name = "keywords"
Tag. Content = Meta_keywords
HEADER.CONTROLS.ADD (TAG)
End If

If not String.IsNullOrEmpty (meta_description) Then
Dim tag as Htmlmeta = New Htmlmeta ()
Tag. Name = "description"
Tag. Content = Meta_description
HEADER.CONTROLS.ADD (TAG)
End If
End Sub


' Get or set the META keyword for the page
Public Property Meta_keywords () property meta_keywords () as String
Get
Return _keywords
End Get
Set
' Remove the extra space
' Translator Note: \s matches any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
_keywords = Regex.Replace (value, "\\s+", "")
End Set
End Property


' Get or set the meta description of the page
Public Property Meta_description () property meta_description () as String
Get
return _description
End Get
Set (ByVal value as String)
' Remove the extra space
' Translator Note: \s matches any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
_description = Regex.Replace (value, "\\s+", "")
End Set
End Property
End Class


The Meta_keywords and Meta_description properties are public, and you can set them after the class is instantiated. When a class inherits from this class and is initialized, Base_init is invoked and the META tag is added to the page

C#

public partial class Home:basepage
{
protected void Page_Load (object sender, EventArgs e)
{

}
}


Vb

Partial Class Homeclass Home
Inherits BasePage
Sub Page_Load () Sub Page_Load (ByVal sender as Object, ByVal e as EventArgs)

End Sub
End Class


Note Each page that inherits from BasePage can be inserted into a META tag by attributes or code. Now we can specify the value of the Meta_keywords and Meta_description properties directly in the @page directive of the. aspx file. Example below

<%@ Page language= "C #" masterpagefile= "~/pagetags.master" autoeventwireup= "true" codefile= "Home.aspx.cs" Inherits = "Home"
Codefilebaseclass= "BasePage"
title= "My home page title"
meta_keywords= "page directive, extension, dotnet, asp.net"
Meta_description= "This is the meta Description for my home page."
%>

<asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >
<p>
This is the content in my home page. This page has a appropriate title tag and
also has meta tags for keywords and description which are to the this page.
The title tag is essential to good search engine optimization and the meta
Description is the text this search engine'll display when your
The page is listed in search results. The title and meta description should be
Set specific to each page and should describe the content of the page.
</p>
</asp:Content>


Note that a CodeFileBaseClass attribute is added here, which is required to refer to the public properties of the BasePage class


Key summary
You should have noticed that regular expressions are used in the BasePage class. This is because it is possible to add descriptions and keywords to your. aspx, like the following example

<%@ Page language= "C #" masterpagefile= "~/ideascope.master" autoeventwireup= "true"
Codefile= "Is.aspx.cs" inherits= "_is"
Codefilebaseclass= "BasePage"

Title= "Effective customer Feedback Management, improve customer commmunication"

meta_keywords= "Customer Feedback, Customer Opinion, Feedback, Opinion,
Idea, ideas, idea management, customer feedback management,
Product management, Product manager, product marketing,
Product Marketing Manager "

Meta_description= "Ideascope is a on-demand and embedded solution that allows
To capture, prioritize and centrally manage customer feedback. Make your
Customer feedback process more efficient. Save time and involve more
Stakeholders without significant cost. "
%>


If you do not convert them with regular expressions, these tags will contain a lot of new lines and spaces, which will make some search engines at a loss, so we have to make these tags convenient search engine collection.

Another problem is that Visual Studio 2005 does not recognize the Meta_keywords property and the Meta_description property. If you specify these two attributes in the @page directive, you will see red wavy lines underneath these properties, VS2005 will think they are invalid, but in fact it can still be compiled and run correctly. If you do not want to see these errors, you can add the following code to the @page instruction in the Visual Studio schema.

<xsd:attribute name= "Meta_keywords" vs:nonfilterable= "true"/>
<xsd:attribute name= "Meta_description" vs:nonfilterable= "true"/>


These nodes should be inserted as <xsd:complextype name= "Pagedef" >, if you install Visual Studio 2005 on the default path, then the path to the schema file is

C:\Program Files\Microsoft Visual Studio 8\common7\packages\schemas\html\page_directives.xsd

This article demonstrates how to support meta keywords and meta descriptions by extending the @page directive. You can also use the same method to add additional meta tags. The original code file and sample project include C # and VB two languages. Thanks to Scott Guthrie's blog post, obsure but cool feature in ASP.net 2.0 provides technical support for this solution.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.