ASP.Net WebForms can do FriendlyUrls too!

With the ASP.Net 4.5 release, we saw the addition of declarative model binding, value providers, unobtrusive validation, and model state. While some of these features were a carry over from the MVC framework, we were still missing that cool SEO friendly friendly URL format.  With the release of ASP.Net 2012.2, Microsoft has delivered on that need.

The FriendlyURLs feature can be added to any ASP.Net WebForms 4 project through the installation of a NuGet package with the command:


Install-Package Microsoft.AspNet.FriendlyUrls -Pre

If you are not currently using routing in your webforms project, you will need to add a call to configure the routing.  If you are familiar with MVC, you should recognize this snippet in global.asax.cs:

RouteConfig.RegisterRoutes(RouteTable.Routes);

The content of your RouteConfig.cs file in the App_Start folder should look something like:

public static class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.EnableFriendlyUrls(new FriendlyUrlSettings()
    {
      AutoRedirectMode = RedirectMode.Permanent,
      CachingMode = CachingMode.Dynamic
    });
  }
}

I have added the configuration options to make redirects from the FriendlyUrl routes permanent (Http 301 status code) and the dynamically cache the contents of the output.  With this configuration in place, I can now route my users to cool looking and SEO friendly URLs like:

I know what you’re thinking:  Jeff, in MVC I get the entries from the request submitted through to action methods as input parameters, how do I access these values from a webform?  The implementation of FriendlyUrls includes an extension method for the Request object called GetFriendlyUrlSegments that returns an IList<string>.  That’s nice, if you really want to iterate over the entire URL and parse apart what was submitted, but I think there is something else you would prefer.

Enter the ValueProviders

ValueProviders are parameter-level attributes that can be used to decorate methods in your webforms.  Using a FriendlyUrlSegments attribute, I can configure a public method in my webform to provide content based on the values submitted on the URL.  Conside this simple webform:

I can use a new feature in ASP.Net 4.5 and bind my Product business object directly to the FormView control.  All I need to do is specify the ItemType and SelectMethod properties to bind data for read operations.  ItemType is the qualified name of the class that is being bound.  SelectMethod is the public method in the code-behind that will return the business object (Product in this sample) to be presented.  Note that I am using the Item keyword to bind to the Product.  This creates a one-way binding, similar to how we use the Eval keyword.  There is also a BindItem keyword available that performs the familiar two-way binding that the Bind keyword gives us.

Let’s look at the GetProduct method:

public static readonly List ProductList = new List()
{
  new Product
  {
    Id=1,
    Name="Chess",
    Description="The classic game - you know... Chess!",
    Price=9.99M
  }
};

public Product GetProduct([FriendlyUrlSegments]int? id)
{
  if (id == null)
    return null;

  return ProductList.FirstOrDefault(p => p.Id == id.Value);


}

Now we see how the FriendlyUrlSegments value provider is put to use.  When the webform is rendered, at the time that my FormView is ready to bind to data.  I don’t need to fuss with event timings, postbacks, or viewstate.  The webform will pass the parameters appropriately from the FriendlyUrl as an input parameter when the FormView is ready to be rendered.  In this case, I end up with a simple webpage that tells me about the Chess product:

Summary

In this article, we introduced the concept of FriendlyUrls in ASP.Net webforms.  I showed you how to retrieve data from the formatted URL string so that it can be consumed.  We also took a brief look at ValueProviders in ASP.Net 4.5 and used the FriendlyUrls attribute with some declarative data-binding on a standard FormView to present some data.

Next time, we’ll dig further into ValueProviders and ModelBinding in ASP.Net 4.5