Using SiteMap and MasterPages to set META Tags in ASP.NET and C#

來源:互聯網
上載者:User

Using the SiteMap for SEO Purposes in ASP.NET 3.5 and C#

Introduction
Especially since the introduction of Web 2.0, a new emphasis has been made on SEO (Search Engine Optimization) for our web pages. Google, amongst others, have managed to organize the world's data so well, that it is becoming more and more important to adhere to such organization techniques to gain the traffic your web site deserves.

In this article we will look at one of the ways we can use the SiteMap in ASP.NET to organize our Web Page more efficiently, and also make it easier for us to update. We will be creating a SiteMap file, which is written in XML, to define our meta Title, Description, Keywords tags as well as page Headers for each of our Web Pages depending upon their location.

What we will learn in this article:

  • How to utilize an ASP.NET SiteMap to harness the power of SEO
  • How to make it easier for us to maintain a multi-page Web Site using a SiteMap

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Getting Started
The major search engines rank a page against serach results based upon many factors. Having matching terms in your meta tags will rank your page higher, particularly in Title, Description and Keywords. However, when maintaining larger sites, this can be difficult to keep organized and a hefty chore to maintain and update. However, we will show how we can make it a little easier using the SiteMap.

To begin, start a new Web Site project in Visual Studio.NET - in this example, we will be using 2008, but 2005 will work just fine.
Once open, right-click your project in the Solution Explorer and choose Add New Item.. SiteMap. Leave the name as Web.sitemap and click Ok. You should have something like this:

<?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="" title="" description="">

<siteMapNode url="" title="" description="" />
<siteMapNode url="" title="" description="" />

</siteMapNode>

</siteMap>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

This is where we start off. But first, let's create some pages to work with. First, let's add a MasterPage: right-click your project and choose Add New Item.. MasterPage. Leave the name as MasterPage.master and then delete the Default.aspx that was added when you created the project.
Now we can add a new Web Form, making sure we check the box to select a Master Page:


[Click to view full-size]

Next, choose the MasterPage we just created:


[Click to view full-size]

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Our next step is to design our MasterPage. For the purpose of this tutorial, we will simply add a Literal control as a placeholder for the page header:

<form id="form1" runat="server">
<strong><asp:Literal ID="lit_PageHeader" runat="server" /></strong>
<div>

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

</div>
</form>

We will also add three more Web Forms to our Project, namely Page1.aspx, Page2.aspx and Page3.aspx and we will identify them in the body, and link to all other pages, like so:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

This is Page1.aspx<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Page2.aspx" Text="Page 2" /> |
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Page3.aspx" Text="Page 3" />

</asp:Content>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

We make sure that we check MasterPage for each of our Web Forms.
The Literal control in our MasterPage is going to display the page header that we specify for each page in the SiteMap. Let's go ahead and begin our SiteMap. It will have somewhat of a hierarchical structure like so:

<?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="~/Default.aspx" title="This is the TITLE of the Default page" PageHeader="Welcome to the Default Page" description="This is the META Description tag od the Default Page." keywords="keywords, of, the, meta, tag, of, the, default, page">

<siteMapNode url="~/Page1.aspx" title="This is the TITLE of Page 1" PageHeader="Welcome to Page 1" description="This is the META Description of Page 1." keywords="keywords, of, the, meta, tag, of, page, one" />
<siteMapNode url="~/Page2.aspx" title="This is the TITLE of Page 2" PageHeader="Welcome to Page 2" description="This is the META Description of Page 2." keywords="keywords, of, the, meta, tag, of, page, two" />
<siteMapNode url="~/Page3.aspx" title="This is the TITLE of Page 3" PageHeader="Welcome to Page 3" description="This is the META Description of Page 3." keywords="keywords, of, the, meta, tag, of, page, three" />

</siteMapNode>

</siteMap>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Notice our attributes for each page in our project. We will be using title for the Meta Title tag, PageHeader for our Literal control, description for the Meta description tag, and keywords for the Meta keywords tag.
To implement this, we will add code-behind to the Master Page:

protected void Page_Load(object sender, EventArgs e)
{

if (SiteMap.CurrentNode["PageHeader"] != null)
{

lit_PageHeader.Text = SiteMap.CurrentNode["PageHeader"].ToString();

}

if (SiteMap.CurrentNode != null)
{

Page.Title = SiteMap.CurrentNode.Title;

if (SiteMap.CurrentNode["keywords"] != null)
{

HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = SiteMap.CurrentNode["keywords"];
Page.Header.Controls.AddAt(1,meta);

}

if (SiteMap.CurrentNode.Description != null)
{

HtmlMeta meta = new HtmlMeta();
meta.Name = "Description";
meta.Content = SiteMap.CurrentNode.Description.ToString();
Page.Header.Controls.AddAt(2,meta);

}

}

}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Now when we run Default.aspx we should be presented with the following in the Title Bar:

And we should also be presented with the Page Header from the Web.sitemap:

Also note that the META tags in the source are populated:

<meta name="Keywords" content="keywords, of, the, meta, tag, of, the, default, page" />
<meta name="Description" content="This is the META Description tag od the Default Page." />

What we have Learned

We have learned that we can use the SiteMap to easily managed and update our Meta tags for each of our pages within our site.

Here is the original link:

http://www.programminghelp.com/web-development/asp-net/using-sitemap-and-masterpages-to-set-meta-tags-in-asp-net-and-c/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.