«

»

2 月
01

ASP.NET how if attribute of dynamic modification H





無標題文件

SEO-ASP.NET how if attribute of dynamic modification Header adds Meta label Keywords Description-SEO Services


Search Engine Optimization

/ / setting key word and page descriptionPublic Void SetMeta(string Key, string Des)
{
HtmlMetaKeywords = New HtmlMeta() , / / KeywordsDescription = New HtmlMeta(); // DescriptionKeywords.Name = “keywords” ;Keywords.Content = Key;
Description.Name = “description” ;Description.Content = Des;

Page.Header.Controls.Add(keywords);
Page.Header.Controls.Add(description);
}

In ASP.NET process designing, because often use a page to show different content through different parameter, because this often needs to realize trends to output different Html Header, for instance Title, keywords, descrtptions.

Commendatory simple way is as follows:
Protected Void Page_Load(object Sender, eventArgs E)
{
/ / Page Title
Page.Title = “This Is A Title And Meta Test Page. Page.Title = “This Is A Title And Meta Test Page.. ;

/ / Encode/Content Type
HtmlMeta Encode = New HtmlMeta();Encode.HttpEquiv = “Content-Type” ;Encode.Content = “text/html; Charset=utf-8” ;
Page.Header.Controls.Add(encode);

/ / Language
HtmlMeta Lang = New HtmlMeta();Lang.HttpEquiv = “Content-Language” ;Lang.Content = “zh-cn” ;
Page.Header.Controls.Add(lang);

/ / Description
HtmlMeta Desc = New HtmlMeta();Desc.Name = “Description” ;Desc.Content = “Test The Meta Controls” ;
Page.Header.Controls.Add(desc);

/ / Keyword
HtmlMeta Keywords = New HtmlMeta();Keywords.Name = “keywords” ;Keywords.Content = “title, meta, test, page” ;
Page.Header.Controls.Add(keywords);

/ / Link/CSS
HtmlLink CssLink = New HtmlLink();CssLink.Href = “MasterPage.css” ;CssLink.Attributes.Add(“rel” , “Stylesheet” );CssLink.Attributes.Add(“type” , “Text/css” );
Page.Header.Controls.Add(cssLink);
}

The page source that when browsing, outputs can achieve following results:

<head><title>
This Is A Title And Meta Test Page.
</title><meta Http-equiv= "Content-Type" Content= "text/html; Charset=utf-8" /><meta Http-equiv= "Content-Language" Content= "zh-cn" /><meta Name= "Description" Content= "Test The Meta Controls" /><<meta Name= "keywords" Content= "title, meta, test, page" /><link Href= "MasterPage.css" Rel= "stylesheet" Type= "text/css" /></head>

Because need the Controls gather of dynamic modification Header, so if put in the user to accuse from the definition in Page_Onload incident because already too late and appear everywhere unusual. General to hoping to use accuse a circumstance that realizes this one function, the proposal packs common kind, be in next MasterPager perhaps is called in the Page_OnLoad of Page can.

#region PAGE HEAD

/ / Page Meta Information
Public Void BackHeadContent(HtmlControl HtmlCtrl)
{
Content(htmlCtrl, “Website tiring-room manages systematic ” , “~/CssStyle/SiteStyles.css” );
}
Public Void HeadContent(HtmlControl HtmlCtrl, string StrPageTitle)
{
Content(htmlCtrl, strPageTitle, “~/CssStyle/Styles.css” );
}
Private Void Content(HtmlControl HtmlCtrl, string StrTitle, string CssFile)
{
/ / Title
HtmlTitle Title = New HtmlTitle();Title.Text = StrTitle;HtmlCtrl.Controls.Add(title);

/ / Link/CSSfile:
HtmlLink CssLink = New HtmlLink();CssLink.Href = CssFile;
&n

Bsp; CssLink.Attributes.Add(“rel” , “Stylesheet” );CssLink.Attributes.Add(“type” , “Text/css” );HtmlCtrl.Controls.Add(cssLink);

HtmlMetaAuthor = New HtmlMeta() , / / AuthorCopyright = New HtmlMeta() , / / CopyrightDate = New HtmlMeta() , / / DateKeywords = New HtmlMeta() , / / KeywordsDescription = New HtmlMeta() , / / DescriptionRobots = New HtmlMeta();// Robots
Author.Name = “Author” ;Author.Content = “Insus.NET” ;
Copyright.Name = “Copyright” ;Copyright.Content = “Copyright 2008 Insus.NET” ;
Date.Name = “date” ;Date.Content = DateTime.Now.ToShortDateString() + ” ” + DateTime.Now.ToShortTimeString();
Keywords.Name = “keywords” ;Keywords.Content = “Insus ” ;
Description.Name = “description” ;
Robots.Name = “robots” ;Robots.Content = “all” ;

String[] InsusWords = “VISUAL STUDIO 2003, VISUAL STUDIO 2005, microsoft SQL Server 2005, ASP.NET, ASP.NET 2.0, VB.NET, c# , AJAX, LINQ “
.Replace(“\\r” , string.Empty)
.Replace(“\\n” , string.Empty)
.Replace(">br />" , string.Empty)
.Replace(” , “, string.Empty)
.Replace(“\\'” , string.Empty)
.Split(‘ ‘);
Foreach (string Word In InsusWords)Keywords.Content += Word + ” , “;If (keywords.Content.ToString().Length>1024)
{Keywords.Content = Keywords.Content.Substring(0, keywords.Content.IndexOf(” ” , 1024));
}
Description.Content = “This Web Site Use Asp.net2.0 And C# And Ajax Technology” ;If (description.Content.ToString().Length>1024)
{Description.Content = Description.Content.Substring(0, description.Content.IndexOf(” ” , 1024));
}
HtmlCtrl.Controls.Add(author);HtmlCtrl.Controls.Add(copyright);HtmlCtrl.Controls.Add(date);HtmlCtrl.Controls.Add(keywords);

HtmlCtrl.Controls.Add(description);HtmlCtrl.Controls.Add(robots);
}

#endregion PAGE HEAD