Microsoft’s latest iteration of its server-side programming language, .NET Framework 4, has new features designed specifically to address SEO shortcomings in previous versions of the Framework. In a recent whitepaper on Microsoft’s ASP.NET website, a number of these features were detailed:
Permanent (“301”) Redirects
Permanent redirects send a visitor from one page to another, but their function in SEO is critical because they tell search engine crawlers requesting the old page to transfer all it’s accumulated rank to the new page, and simultaneously drop it from the search engine’s results.
Returning a “301” response in the header tells the requestor that this redirection is permanent (a “302” response tells the requestor that the page is temporarily unavailable, which does not help SEO).
Prior to Framework 4, permanent “301” redirects were accomplished in one of two ways, either injecting the response object headers into the page before it’s sent back to the requestor (using “response.addheader”), or using IIS 7’s optional URLRewrite module (prior to IIS 7, rewriting was usually performed by a 3rd-party ISAPI module). Now permanent redirects can be handled in a single line of code:
RedirectPermanent("/newpath/foroldcontent.aspx");
If your website uses dynamic pages built from a database, this new syntax makes it incredibly simple to manage site upgrades and URL changes. If you’re trying to permanently redirect static content to new pages, URLRewrite maps are still your best friend (now directly supported by .NET 4’s routing engine).
Setting Keywords and Description META tags
While the keywords META tag has pretty much fallen out of use on major search engines, the META Description tag is still one of the most important elements of SEO, since it’s content is both used by search engines and displayed in search results. It plays a critical role in click-through rates and needs to correspond closely with the page content. It also needs to be unique to each page it appears on, or it will negatively impact search rank.
.Net Framework 4 provides two new methods for adding META tags at runtime (before the page has been sent to the requestor). You can now add them directly to the “@Page” directive at the top of every ASPX page, along with the page TITLE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Keywords="These, are, my, keywords"
Description="This is a description" %>
When the page is rendered and sent back to the browser, these elements will be correctly rendered:
<head id="Head1" runat="server">
<title>Untitled Page</title>
<meta name="keywords" content="These, are, my, keywords" />
<meta name="description" content="This is the description of my page" />
</head>
Prior to Framework 4, programmers commonly manually inserted this content using placeholders, or overloaded the “header” declaration in the page object. Now, the page object itself has been extended with specfic methods, making for a one-line solution:
Page.MetaKeywords = “My, keywords”
Page.MetaDescription = “My Description”
For dynamically generated content, this is a tighter and less error-prone method, and allows for a complete separation of programming and page design.
Improved Browser Capability Providers
Another frustration for .NET developers has been browser compatibility. If you’re designing pages to display properly on mobile devices as well as desktop computers, the “.browser” file tells .NET how to compile the pages for a specific browser. This process has been streamlined in .NET using the new, cachable and extensible HttpCapabilitiesProvider.
Replacing URLRewrite with Routing
Most web developers have a love-hate relationship with Microsoft’s URLRewrite Module, which allows rewriting and redirection rules to be applied before pages are compiled. While IIS 7 provided a simple rewrite-rule generator, it was accessed through the IIS Admin interface, limiting access to developers using shared hosting environments.
Routing support existed in .NET 3.5 sp1, but it has been simplified and improved in .NET 4 with the following features:
- The PageRouteHandler class, which is a simple HTTP handler that you use when you define routes. The class passes data to the page that the request is routed to.
- The new properties HttpRequest.RequestContext and Page.RouteData (which is a proxy for theHttpRequest.RequestContext.RouteData object). These properties make it easier to access information that is passed from the route.
- The following new expression builders, which are defined inSystem.Web.Compilation.RouteUrlExpressionBuilder andSystem.Web.Compilation.RouteValueExpressionBuilder:
- RouteUrl, which provides a simple way to create a URL that corresponds to a route URL within an ASP.NET server control.
- RouteValue, which provides a simple way to extract information from the RouteContext object.
- The RouteParameter class, which makes it easier to pass data contained in a RouteContext object to a query for a data source control (similar to FormParameter).
After creating a public class for the routing object, .NET 4’s “MapPageRoute” method simplifies
the syntax back down to a one-line statement:
RouteTable.Routes.Add("SearchRoute", new Route("search/{searchterm}", new PageRouteHandler("/search.aspx")));
This example sets up a route mapping an SEO-friendly search URL (e.g.: “mySite.com/search/widget”) to a physical page, and sets the search term as the first parameter. On your search page, you can capture the requested product in a single line as well, either in your code-behind:
string searchterm = Page.RouteData.Values["searchterm"] as string; label1.Text = searchterm;
or in the page directly:
<asp:Label ID="Label1" runat="server" Text="<%$RouteValue:SearchTerm%>" />
With the new routing syntax, friendly URLs have never been easier!