Standard ASP.NET Web Forms SEO Friendly Routing

StandardASPNETRouting

ASP.NET MVC gets all the credit currently for having SEO friendly routing as part of its inherent implementation.  However, similar routing capability was implemented into Standard ASP.NET (Web Forms) 4.0 and is part of all the later 4.x versions as well.  It is also implemented in a fairly similar manner.

So for example if we look at a typical ASP.NET MVC routing setup we find that it is also part of and MVC project’s “Global.asax” page, which is the same for Standard ASP.NET.   Interestingly enough, ASP.NET MVC routing looks a little more complex than that which is found in Standard ASP.NET as we can see below…

ASP.NET MVC Routing Example (Global.asax)

Sub Application_Start()

    AreaRegistration.RegisterAllAreas()

    RegisterGlobalFilters(GlobalFilters.Filters)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)

    filters.Add(New HandleErrorAttribute())
End Sub

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    routes.MapRoute( _
                     "Administration_MasterMenu", _
                     "Administration", _
                     New With { .controller = "Administration", .action = "Index", .id = "" } _
                   )

    routes.MapRoute( _
                     "Documents_MasterMenu", _
                     "Documents", _
                     New With { .controller = "Documents", .action = "Index", .id = "" } _
                   )

    ' default route
    routes.MapRoute( _
                     "Default", _
                     "{controller}/{action}/{id}", _
                     New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
                   )
End Sub

The example above is a fairly straight-forward setup for some routing in one of Black Falcon Software’s earlier test-bed ASP.NET MVC applications.  As you will see there is a little more to consider with the MVC example.  However, the MVC model allows for some fairly complex routing.

Standard ASP.NET on the other hand can in fact accommodate complex routing but it appears to be a much more straight forward implementation as the example below demonstrates…

Standard ASP.NET Routing Example (Global.asax)

Public Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

     RegisterRoutes(RouteTable.Routes)
End Sub

Private Sub RegisterRoutes(poRoutes As RouteCollection)

     poRoutes.RouteExistingFiles = False
     poRoutes.Add(new Route("Images/", new StopRoutingHandler()))

     poRoutes.MapPageRoute("Home", "Home/", "~/Aspx/Home.aspx")
     poRoutes.MapPageRoute("About", "About/", "~/Aspx/About.aspx")
     poRoutes.MapPageRoute("Products", "Products/", "~/Aspx/Products.aspx")
     poRoutes.MapPageRoute("ProductScreenshots", "ProductScreenshots/", "~/Aspx/ProductScreenshots.aspx")
     poRoutes.MapPageRoute("ProductScreenshot", "ProductScreenshot/{refid}", "~/Aspx/ProductScreenshot.aspx")
     poRoutes.MapPageRoute("Services", "Services/", "~/Aspx/Services.aspx")
     poRoutes.MapPageRoute("Software", "Software/", "~/Aspx/Software.aspx")
     poRoutes.MapPageRoute("Articles", "Articles/", "~/Aspx/Articles.aspx")
     poRoutes.MapPageRoute("Links", "Links/", "~/Aspx/Links.aspx")
     poRoutes.MapPageRoute("Contact", "Contact/", "~/Aspx/Contact.aspx")
     poRoutes.MapPageRoute("Customer", "Customer/{productid}", "~/Aspx/Customer.aspx")
     poRoutes.MapPageRoute("CustomerThankYou", "CustomerThankYou/", "~/Aspx/CustomerThankYou.aspx")
     poRoutes.MapPageRoute("CustomerCancel", "CustomerCancel/", "~/Aspx/CustomerCancel.aspx")
     poRoutes.MapPageRoute("Error", "Error/", "~/Aspx/Error.aspx")
     poRoutes.MapPageRoute("TestPage", "TestPage/", "~/Aspx/TestPage.aspx")
End Sub

In the example above we should note that there are three route-parameters that have to be considered as follows…

poRoutes.MapPageRoute("Route-Name", "SEO Friendly Route”, "Actual Route to ASPX page”)

In the case where you require a parameter to be sent with a route request you can set up such a route as follows…

poRoutes.MapPageRoute("Route-Name",  "SEO Friendly Route/{param-name}”,  "Actual Route to ASPX page”)
poRoutes.MapPageRoute("Route-Name",  "SEO Friendly Route/{param1-name}/{param2-name}”,  "Actual Route to ASPX page”)

Ensure that you surround your parameter name(s) with braces as shown above.

To retrieve your parameter values add the following code as necessary into the route target’s ASPX’s “Page_Load” event…

Dim lsProductId  As String = ""

lsProductId = Page.RouteData.Values("productid")

Routing in Standard ASP.NET is quite helpful in making your ASP.NET web application SEO friendly while also presenting cleaner looking routes to a user.

There is one downside to routing in either ASP.NET implementation; it does incur reflection internally, which tends to incur performance penalties and in fact is a major bottleneck in ASP.NET MVC applications when extensive routing has been implemented. As a result do not be overly ambitious when developing your routes and keep them as simple as possible.

For further research on the use of routing in Standard ASP.NET web applications you may review the material at the following Microsoft links…

https://msdn.microsoft.com/en-us/library/vstudio/dd329551(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/cc668201(v=vs.140).aspx

 

Leave a comment