Page 1 of 1

Adding <Title> and <Meta> tags to some pages

Posted: Mon Mar 02, 2009 7:50 am
by Mike718NY
How can I add <title> and <meta> tags for the Default.aspx page
and the custom pages I made?
Right now it's showing: <title>Home Page</title> or <title>MyCustomPageName</title>.

I want to add title and meta tags mainly because I want a better title and description
than what is showing up in the search engine listings.

Also, when I click on a Category on the left menu, it shows the
name of the category in the <title> tag, ex:
<title>Blood Pressure</title>
but I would like to add other words to it, like the websites domain name.
There are also no <meta> tags for these pages.
How can I add <meta> tags to the Category pages? Thanks

Re: Adding <Title> and <Meta> tags to some pages

Posted: Mon Mar 02, 2009 8:09 am
by mazhar
Helpful posts
viewtopic.php?f=42&t=7875
viewtopic.php?f=42&t=9073

In order to adjust category name in title you need to check the following statements in the code of category display pags

Code: Select all

Page.Title = _Category.Name;

Re: Adding <Title> and <Meta> tags to some pages

Posted: Mon Mar 02, 2009 9:53 am
by Mike718NY
thanks mazhar, but I'm still confused at what code I should use
because those posts contain many things.

Is it this - (In all root pages of website for example Default.aspx, ContactUs.aspx etc):

protected override void Render(HtmlTextWriter writer)
{
string pageHtml = string.Empty;

using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
base.Render(hw);
pageHtml = sw.ToString();

if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
{
int headStartIndex = pageHtml.IndexOf("<head");
int headEndIndex = pageHtml.IndexOf("</head>");

string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

StringBuilder sb = new StringBuilder(headerPart);
sb.Replace("<link", "\r<link");
sb.Replace("<Meta", "\r<Meta");
sb.Replace("<style", "\r<style");

int sindex = headerPart.IndexOf("<title>");
int eindex = headerPart.IndexOf("</title>");
string titleTag = headerPart.Substring(sindex, (eindex + 8 ) - sindex);
string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
titleText = titleText.Replace("\r", string.Empty);
titleText = titleText.Replace("\t", string.Empty);
titleText = titleText.Replace("\n", string.Empty);
titleText.Trim();
titleText = String.Format("\r<title>{0}</title>", titleText);
sb.Replace(titleTag, titleText);
sb.Replace("</head>", "\r</head>");
string newHeaderPart = sb.ToString();

//find out the index of first meta tag
int metaIndex = newHeaderPart.IndexOf("<Meta", StringComparison.CurrentCultureIgnoreCase);

//meta tag found
if (metaIndex >= 0)
{
//remove the current title information
string updatedHeaderPart = newHeaderPart.Replace(titleText, string.Empty);

//inject the title information at first meta tag index
newHeaderPart = updatedHeaderPart.Insert(metaIndex-1, titleText);
}
pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
}
hw.Close();
}
sw.Close();
}
writer.Write(pageHtml);
}

Re: Adding <Title> and <Meta> tags to some pages

Posted: Mon Mar 02, 2009 10:40 am
by mazhar
I think for meta tag it would be better to use the first solution proposed in first post
viewtopic.php?f=42&t=7875

Re: Adding <Title> and <Meta> tags to some pages

Posted: Mon Mar 02, 2009 11:30 am
by Mike718NY
do you mean this method:

2. go to the store page you want to set meta tags for
3. toggle "edit page" in footer
4. observe what is being used as "active layout"
5. go to admin
6. go to website _> content and layout
7. locate the layout scriptet in use for the page
.........

For the Default.aspx, the "active layout" says "Left Sidebar".

Should this be the "Content" page instead, that says "HomePage"?