Wednesday, November 28, 2012

MVC and SEO basics: inject title, keywords and description into views

Almost by accident, I came across Google's starter guide for SEO optimisation and decided that it is a good idea to make some improvements I've been neglecting. Here's what I did so far and how I applied it to the MVC framework.

1. Create unique, accurate page titles

One way to do it with the MVC framework is to create a placeholder on the master page and then override it on the view page.




       Home Page

For now, I chose the easier approach to set the title in the _Layout.cshtml


And assign it in each view separately

    ViewBag.Title = "Main Page - The Stepping Stone Markov Chain Algorithm - MVC Stepping Stone Example";

2. Make use of the "description" and "keywords" meta tags

This takes a little more work. Here's my chosen approach: First, make sure each controller inherits from the BaseController. Then create two new classes, MetaDescriptionAttribute and MetaKeywordsAttribute, and inherit them from System.Attribute

public class MetaDescriptionAttribute : Attribute
 private readonly string _parameter;

 public MetaDescriptionAttribute(string parameter)
  _parameter = parameter;

 public string Parameter { get { return _parameter; } }

public class MetaKeywordsAttribute : Attribute
 private readonly string _parameter;

 public MetaKeywordsAttribute(string parameter)
  _parameter = parameter;

 public string Parameter { get { return _parameter; } }

In BaseController, override OnActionExecuting

protected override void OnActionExecuting(ActionExecutingContext filterContext)
 var keywords = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaKeywordsAttribute), false);
 if (keywords.Length == 1)
  ViewData["MetaKeywords"] = ((MetaKeywordsAttribute)(keywords[0])).Parameter;

 var description = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaDescriptionAttribute), false);
 if (description.Length == 1)
  ViewData["MetaDescription"] = ((MetaDescriptionAttribute)(description[0])).Parameter;


Decorate the appropriate controller method with newly created attributes

[MetaKeywords("C#, MVC, Markov Chain, Stepping Stone, Programming")]
[MetaDescription("Stepping Stone Markov Chain model is an example that has been used in the study of genetics. In this model we have an n-by-n array of squares, and each square is initially any one of k different colors. For each step, a square is chosen at random. This square then chooses one of its eight neighbors at random and assumes the color of that neighbor")]
public ActionResult Index()
 HtmlString table = new HtmlString(SteppingStoneHelpers.table.ToString());
 return View(table);

Finally, in the _Layout.cshtml, add the following

All done! There we go, the resulting html:

    Main Page - The Stepping Stone Markov Chain Algorithm - MVC Stepping Stone Example


Google Starter Guide
ASP.NET MVC - View with master page, how to set title? mvc - strategy for including SEO information such as meta keywords and descriptions
by . Also posted on my website


Web design company said...

It includes complete details and importance of the on page optimization.There are so many factors in the on page optimization.SEO Firm Bangalore

Martina raj said...

Your work article, blogs I mean over all contents is must read stuff..
Marble Fireplaces Wakefield