by Patrick
7. November 2008 14:11
So today's challenge was to display the Title, Meta description and meta tags on a site I am currently working on for troubleshooting and SEO reasons.
The solution I came up with was to place the following code in the code behind of the masterpage.
protected void Page_Load(object sender, EventArgs e)
{
HtmlHead headTag = (HtmlHead)Page.Header;
Response.Write("Title of Page: " + headTag.Title + "");
foreach (var control in Page.Header.Controls)
{
var test = control.GetType();
if (test.Name == "HtmlMeta")
{
if ((control as HtmlMeta).Name == "Description")
{
Response.Write("MetaDescription : " + (control as HtmlMeta).Content + "");
}
if ((control as HtmlMeta).Name == "Keywords")
{
Response.Write("MetaKeywords : " + (control as HtmlMeta).Content + "");
}
}
}
}
The HeadTag.Title gives us the Title of the current page.
The foreach loop jumps through each control in the header section of the current page.
Since there can be different types of controls in the header section, we need to check for the type of HtmlMeta.
Then we need to check to see if it is the Description or Keywords and return the content values.
I am sure there are other ways, but this is a really simple way to display the 3 main SEO elements, Page Title, Page MetaTag Description and Page MetaTag Keywords for all the ASP.NET pages that use this master page (or you can place it on each page code behind) without having to view the source code.
Just remember to remove this code before you launch your site :)
by Patrick
1. November 2008 19:11
Ok,
My rant for the day is about trying to use Microsoft C# and LINQ and XML and WPF. I spend more time trying to figure out simple stuff, because nothing exists as solid examples of how to do what I need to do.
I spend so much of my time trying to find decent examples of how to do stuff on the net, and find nearly nothing.
Why?
I know people are doing this stuff, is it because its a closed source system and sharing the code just isn't "cool" by the users?
I am sick of it, and I am going to try to lead by example and post my minor victories in trying to do things using this great platform.
Why?
Well, couple of reasons...
1. Code should be free. Not as in not get paid for it, but how to do something should be shared, knowledge isn't worth anything unless you share it.
2. Selfish reasons, I can find different things I find much easier and watch my progression through the products
3. Cause I haven't really found a good reason to keep posting regularly on my blog, and this just might be a good excuse.
So, there you have it, my rant for the day and my promise to share my code examples of what I am trying to do.