Click here to Skip to main content
Click here to Skip to main content

ASP.Net WebForms can do FriendlyUrls too!

, 17 Feb 2013
Rate this:
Please Sign up or sign in to vote.
I n this article, I'll introduced the concept of FriendlyUrls in ASP.Net webforms. We'll see how to retrieve data from a formatted url string so that it can be consumed. We'll also take a brief look at ValueProviders in ASP.Net 4.5 to present some data.

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<product> ProductList = new List<product>()
{
  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);


}
</product></product>

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

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Jeffrey T. Fritz
Telerik
United States United States
A Microsoft MVP, ASPInsider and ASP.NET Developer Evangelist for Telerik. Jeffrey is a software developer coach, architect, and speaker in the Microsoft.Net community. A Pluralsight author and international speaker, Jeffrey makes regular appearances at conferences such as TechEd, DevIntersection, CodeStock, FalafelCon, DevReach and New York Code Camp as well as user group meetings in an effort to grow the next generation of software developers
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionProblems to display mobile version with FriendlyUrls PinmemberLicey23-Apr-14 2:28 
AnswerRe: Problems to display mobile version with FriendlyUrls PinpremiumJeffrey T. Fritz5-May-14 3:55 
GeneralRe: Problems to display mobile version with FriendlyUrls PinmemberLicey21-May-14 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 17 Feb 2013
Article Copyright 2013 by Jeffrey T. Fritz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid