I am using master pages. I want to put different head and meta (description, keywords) on each content page. How can this be done to make my site better designed for search engines.
I'm still new to master pages, but I know of two ways to control a master pages title or meta tags. One was mentioned in a previous post (adding a content placeholder to the head section of the master page). Here is another that someone showed to me:
The following code in the code behind file of the master page:
HtmlMeta theMetaTag = new HtmlMeta();
theMetaTag.Attributes.Add("name", "test");
theMetaTag.Attributes.Add("content", "this is a test");
Page.Header.Controls.Add(theMetaTag);
will generate this tag in the head section of the page:
<meta name="test" content="this is a test" />
In order to give more control (if that is necessary), create a public property in the master page that controls this meta tag. For instance, I have worked on pages where we have boolean properties to tell whether or not an optional meta tag is rendered, or
string properties to tell what should go in the content attribute of a required meta tag.
So if you want the meta tag to be optional, add this property to the master page:
public property bool UseMetaTag
{
get
{
object o = ViewState["usemetatag"];
return o==null?string.empty:(string)o;
}
set
{
ViewState["usemetatag"] = value;
}
}
(I am doing this from memory so I hope I got that right.)
Then you just set it in the content page's code behind:
Master.UseMetaTag = true;
and make the code above conditional:
if (this.UseMetaTag)
{
HtmlMeta theMetaTag = new HtmlMeta();
...
}
As far as setting the title dynamically, I created this property in a master page and set it in the content page:
public String PageTitle
{
get
{
return Page.Header.Title;
}
set
{
Page.Header.Title = value;
}
}
I hope this is at least somewhat helpful:)
I would, of course, be interested if someone knows of a better or different way.
Hi, thanks for all the info. I am still having problems integrating this into my site. My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc. Would it be possible for you to spell it out for me where the code
goes exactly. Thanks for your patience>
Hi, thanks for all the info. I am still having problems integrating this into my site. My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc. Would it be possible for you to spell it out for me where the code
goes exactly. Thanks for your patience>
I have been playing a bit with this and you can try something like this in the page load of contact.aspx page:
Dim myMeta = New HtmlMeta
myMeta.name = "description"
myMeta.content = "This is our contact page"
Page.Header.Controls.Add(myMeta)
Odd enough i didnt get any intellisense for myMeta [:^)].
I dont know whether its a good way of doing it but I added a content placeholder in the head tag of the master page which I then linked to in each of my content pages. It might not work very well for search engines though because the content doesnt sit in
the head tags on the content pages because the head tags are located on the master page. Even though when the page is compiled and you go view source on the page when viewing it in the web browser it does show up in the head tags. However, if google doesnt
compile the page before crawling it then this wont happen if you see what i mean.
> I dont know whether its a good way of doing it but I added a content placeholder in the head tag of the master page which I then linked to in each of my content pages.
IMO, this is a great solution!
> It might not work very well for search engines though because the content doesnt sit in the head tags on the content pages because the head tags are located on the master page.
This problem actually does not exist and your pages are fine. Consider that master and content get merged before being sent back to the browser or crawler, so it just looks like a regular page and the client has not even way to ascertain it is coming from
a page implementing a master...
ajw
Participant
768 Points
699 Posts
Different head and meta tag content on each content page
Aug 31, 2006 09:31 AM|LINK
Hi,
I am using master pages. I want to put different head and meta (description, keywords) on each content page. How can this be done to make my site better designed for search engines.
Thanks
Andrew Welch
Abtec Network Systems
hoopslife
Contributor
2279 Points
444 Posts
Re: Different head and meta tag content on each content page
Aug 31, 2006 02:46 PM|LINK
Microsoft Certified Solutions Developer
Blog
ajw
Participant
768 Points
699 Posts
Re: Different head and meta tag content on each content page
Aug 31, 2006 03:03 PM|LINK
I am really more interested in having a different title and meta keywords and description on each page or for each page rather than styles.
Cheers
Andrew
aspnetprogra...
Member
85 Points
17 Posts
Re: Different head and meta tag content on each content page
Aug 31, 2006 10:56 PM|LINK
I'm still new to master pages, but I know of two ways to control a master pages title or meta tags. One was mentioned in a previous post (adding a content placeholder to the head section of the master page). Here is another that someone showed to me:
The following code in the code behind file of the master page:
HtmlMeta theMetaTag = new HtmlMeta();
theMetaTag.Attributes.Add("name", "test");
theMetaTag.Attributes.Add("content", "this is a test");
Page.Header.Controls.Add(theMetaTag);
will generate this tag in the head section of the page:
<meta name="test" content="this is a test" />
In order to give more control (if that is necessary), create a public property in the master page that controls this meta tag. For instance, I have worked on pages where we have boolean properties to tell whether or not an optional meta tag is rendered, or string properties to tell what should go in the content attribute of a required meta tag.
So if you want the meta tag to be optional, add this property to the master page:
public property bool UseMetaTag
{
get
{
object o = ViewState["usemetatag"];
return o==null?string.empty:(string)o;
}
set
{
ViewState["usemetatag"] = value;
}
}
(I am doing this from memory so I hope I got that right.)
Then you just set it in the content page's code behind:
Master.UseMetaTag = true;
and make the code above conditional:
if (this.UseMetaTag)
{
HtmlMeta theMetaTag = new HtmlMeta();
...
}
As far as setting the title dynamically, I created this property in a master page and set it in the content page:
public String PageTitle
{
get
{
return Page.Header.Title;
}
set
{
Page.Header.Title = value;
}
}
I hope this is at least somewhat helpful:)
I would, of course, be interested if someone knows of a better or different way.
lostlander
Contributor
3041 Points
607 Posts
Re: Different head and meta tag content on each content page
Sep 01, 2006 02:29 AM|LINK
Here is an article you can follow:
In the section: Headers, Scripts, and Meta Tags, Too
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
It details the process of adding Headers and other things.
Hope it helps! ^_^
ajw
Participant
768 Points
699 Posts
Re: Different head and meta tag content on each content page
Sep 01, 2006 11:13 AM|LINK
Hi, thanks for all the info. I am still having problems integrating this into my site. My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc. Would it be possible for you to spell it out for me where the code goes exactly. Thanks for your patience>
Andrew
ewschone
Member
49 Points
11 Posts
Re: Different head and meta tag content on each content page
Sep 03, 2006 12:37 PM|LINK
Hi, thanks for all the info. I am still having problems integrating this into my site. My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc. Would it be possible for you to spell it out for me where the code goes exactly. Thanks for your patience>
I have been playing a bit with this and you can try something like this in the page load of contact.aspx page:
Dim myMeta = New HtmlMeta
myMeta.name = "description"
myMeta.content = "This is our contact page"
Page.Header.Controls.Add(myMeta)
Odd enough i didnt get any intellisense for myMeta [:^)].
ajw
Participant
768 Points
699 Posts
Re: Different head and meta tag content on each content page
Sep 04, 2006 08:19 AM|LINK
LudovicoVan
Star
9682 Points
1935 Posts
Re: Different head and meta tag content on each content page
Sep 12, 2006 11:16 AM|LINK
ajw:
> I dont know whether its a good way of doing it but I added a content placeholder in the head tag of the master page which I then linked to in each of my content pages.
IMO, this is a great solution!
> It might not work very well for search engines though because the content doesnt sit in the head tags on the content pages because the head tags are located on the master page.
This problem actually does not exist and your pages are fine. Consider that master and content get merged before being sent back to the browser or crawler, so it just looks like a regular page and the client has not even way to ascertain it is coming from a page implementing a master...
Hope this clearifies. -LV
master Head
chazcross
Member
26 Points
5 Posts
Re: Different head and meta tag content on each content page
Jan 17, 2007 05:29 PM|LINK
i went a different route.
In the head of the masterpage i added a id and runat tag
<meta id="description" runat="server" name="description" content="Default description here" />
And for every page where i needed to modify this tag i used
HtmlMeta description = (HtmlMeta)Page.Header.FindControl("description"); description.Content = "description here";