Use the meta tag in Asp.net 2.0 with the motherboard page (Extended @ page command)

Source: Internet
Author: User

Introduction
The dashboard page is a very powerful feature in ASP. NET 2.0, but it cannot provide the most basic method for optimizing search engines. If you want your web page to be indexed by the search engine and improve the ranking, You need to specify a title and meta tag on each page. This article describes how to expand your asp.. NET page, so that you can directly specify the description of your meta tag and the keywords of the meta tag in the @ page command on your content page.

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

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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
Your money with rhinoback! "/>
<Meta name = "keywords" content = "backup, online backup, secure backup, cheap backup,
Free backup, offsite backup, inte. Net backup, secure files, offsite data storage,
Privacy, security, features, low prices, Premium Service, remote backup "/>
</Head>
<Body>
<! -- Page content -->
</Body>
</Html>

<Title> the label text is displayed at the top of the browser. In the following example, we can see that <title> is displayed in IE.

When your page is indexed by the search engine, the meta description text is displayed in the search engine list. The following example is from Google. The text below the underlined title comes from the meta description tag of the page. If there is no meta description tag, your page will be displayed as a text on your page in the search engine list. Specifying the description text of each page is much better than handing over these things to the search engine.

The dashboard page has been proved to be a very useful feature in ASP. NET 2.0. This article does not explain the details of the Parent Board page or how to implement it, becauseArticleToo many. If we use

<% @ Page Language = "C #" masterpagefile = "~ /Pagetags. Master "autoeventwireup =" true "codefile =" home. aspx. cs "inherits =" home "Title =" my home page title "%>

The above @ page command shows that this is a content page that uses the mother board page. If you want to specify the meta tag, you should specify it on the Content Page. You can see that the @ page command has a "Description" attribute, but it cannot create a meta description tag on your page. In fact, even if you specify the "Description" attribute, it will be ignored and will not do anything.

I cannot allow all pages of the site to use the same description, and I want to add a keyword to each page. The solution we first came up with isCodeInsert the meta tag we want into the C #

Protected void page_load (Object sender, eventargs E)
{
Htmlmeta tag = new htmlmeta ();
Tag. Name = "Description ";
Tag. content = "my description for 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 for this page"
Header. Controls. Add (TAG)
End sub

One problem with this solution is that the title, Meta description, and META keywords of the page are correlated. Therefore, we 'd better specify the title and description in the same file. In the page_load method, you can easily add a <SCRIPT> label to the. ASPX page, but I want a solution that is simpler to set and check the labels on each page.

In the following solution, we will see how to add a meta tag to each page by extending the @ page command.

Solution
I created a base page class that inherits from system. Web. UI. Page and inherited my content page from the basepage class. The basepage class is included in. add the meta tag code to the header control on the ASPX page. After we inherit the basepage, this code only needs to exist in one place, instead of writing every page.
C #

Using system;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Text. regularexpressions;

/** // <Summary>
/// Extend the base class to add meta tags to the content page
/// </Summary>
Public class basepage: Page
{
Private string _ keywords;
Private string _ description;
// Constructor
// Add an init event for processing
Public basepage ()
{
Init + = new eventhandler (basepage_init );
}

// The page will be initialized using this base class
// If available, add the meta keyword and meta description.
Void basepage_init (Object sender, eventargs E)
{

If (! String. isnullorempty (meta_keywords)
{< br> htmlmeta tag = new htmlmeta ();
tag. name = "keywords";
tag. content = meta_keywords;
header. controls. add (TAG);
}

If (! String. isnullorempty (meta_description)
{< br> htmlmeta tag = new htmlmeta ();
tag. name = "Description";
tag. content = meta_description;
header. controls. add (TAG);
}< BR >}

/** //


// obtain or set the meta keyword on the page
///
Public string meta_keywords
{< br> Get
{< br> return _ keywords;
}< br> set
{< br> // Delete unnecessary spaces
// Note: \ s matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v].
_ KEYWORDS = RegEx. Replace (value, "\ s +", "");
}< BR >}

/** // <Summary>
/// Get or set the meta description on the page
/// </Summary>
Public String meta_description
{
Get
{
Return _ description;
}
Set
{
// Delete unnecessary Spaces
// Translator's note: \ s matches any blank characters, including spaces, tabs, and page breaks. It is 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

'The base class is extended to add meta tags to the content page

Public class basepageclass basepage
Inherits page

Dim _ keywords as string
Dim _ description as string
'Page will be initialized using this base class
'Add an init event handler
Public sub new ()
Addhandler init, new eventhandler (addressof basepage_init)
End sub

'Page will be initialized using this base class
'If available, the meta keyword and meta description are added.
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 on the page
Public Property meta_keywords () as string
Get
return _ keywords
end get
set
'remove unnecessary spaces
'Note: \ s matches any blank characters, including spaces, tabs, and page breaks. It is 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 unnecessary Spaces
'Note: \ s matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v].
_ Description = RegEx. Replace (value, "\ s + ","")
End set
End Property
End Class

Meta_keywords attributes and meta_description attributes are public. You can set them after class instantiation. When a class inherits from this class and is initialized, base_init is called and meta tags are 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 that the meta tag can be inserted through attributes or code on each page inherited from the basepage. Now we can directly specify the values of meta_keywords and meta_description in the @ page command of the. aspx file. Example:

<% @ 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, do. net, Asp. net "
meta_description =" this is the meta description for my home page. "
%>

<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
<H3> my home page content <P>
This is the content on my home page. This page has an appropriate title tag and
Also has meta tags for keywords and description that are relative to this page.
The title tag is essential to good search engine optimization and the Meta
Description is the text that the search engine will display when your
Page is listed in search results. The title and meta description shocould be
Set specific to each page and shoshould describe the content of the page.
</P>
</ASP: content>

Note that a codefilebaseclass attribute is added here, which is required and can reference the public attributes of the basepage class.

Highlights
You should note that regular expressions are used in the basepage class. This is because when you add descriptions and keywords in your. aspx, there may be multiple rows, just like in the following example.

<% @ Page Language = "C #" masterpagefile = "~ /Ideasloud. Master "autoeventwireup =" true"
Codefile = "is. aspx. cs" inherits = "_ is"
Codefilebaseclass = "basepage"

Title = "proactive 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 an on-demand and embedded solution that allows
You 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 use regular expressions to convert them, these tags will contain many new lines and spaces, which will make some search engines overwhelmed, so we need to make these tags easy for search engines to include.

Another problem is that Visual Studio 2005 does not know the meta_keywords and meta_description attributes. If you specify these two attributes in the @ page command, red lines will appear below these attributes, and vs2005 considers them invalid, but in fact it can still be correctly compiled and run. If you do not want to see these errors, you can add the following code to the @ page command in the schema of Visual Studio.

<XSD: attribute name = "meta_keywords" vs: nonfilterable = "true"/>
<XSD: attribute name = "meta_description" vs: nonfilterable = "true"/>

These nodes should be inserted as sub-nodes of <XSD: complextype name = "pagedef">. If you install Visual Studio 2005 in the default path, the path of the schema file is

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

This article demonstrates how to enable the @ page command to support META keywords and meta descriptions. You can also add other meta tags using the same method. The source code file and example project are in the C # and VB languages. Thanks to Scott Guthrie for his blog post ,.. Net/scottgu/archive/2005/08/02/421405. aspx "target = _ blank> obsure but cool feature in ASP. NET 2.0 provides technical support for this solution.

This article Reprinted from the network base camp: http://www.pushad.com/Info/10377.Html

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.