ASP.NET MVC SEO - Solution Part 1
In a previous post I explained some of the issues with ASP.NET MVC when trying to implement an SEO-optimized web site. In this post I'll begin to explore some possible solutions.
Step 1: Master View - some additions
First let's make it easy to set the meta description
, page title
,
meta keywords
and canonical url
by adding the following to the
head
section of the master view:
<head id="Head1" runat="server">
<title><%=ViewData["PageTitle"]%></title> <%-- This gets wrapped here, so it sees a title tag and doesn't emit two --%>
<%=ViewData["PageDescription"]%> <%-- These are wrapped elsewhere so they vanish if not set--%> <%=ViewData["PageKeywords"]%> <%-- These are wrapped elsewhere so they vanish if not set--%>
<%=ViewData["CanonicalUrl"]%> <%-- These are wrapped elsewhere so they vanish if not set--%>
<meta name="robots" content="noodp" /> <%--Don't use Open Directory Project descriptions--%>
Note how we are not wrapping the canonical URL, and meta tags around the ViewData here (even though it would be more correct to do so). We do this so that when those tags are not present the entire tag disappears from the page instead of rendering a tag with an empty string in it. For the title tag however, that's universal so let's do it 'properly'.
In tomorrow's post I'll show how we can set the Canonical URL using an attribute.