+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Adding Custom Route for Static Web Forms In Sitefinity Sites

by Stanislav Velikov

Today's bog post deals with the concept of using ASP.NET Routing in your Sitefinity application. Typically in ASP.NET an incoming request for a URL typically maps to a physical file handling the request (e.g. an .aspx file). However in some use case scenarios you might want to define URLs that do not have to map to specific files in your Web site - this is when you need to use ASP.NET Routing. Recently we got a request from our customer to help them register their custom routes with Sitefinity. The purpose of the below post is not to explain the concepts and mechanisms behind routing,a s that would simply repeat the above linked MSDN resource but rather demonstrate how you can apply your knowledge about routing and register custom routes with Sitefinity, as the approach differs slightly from the standard ASP.NET approach.

While in ASP.NET you would register your routes directly to the RouteTable.Routes collection on Application_Start in your Global.asax, this is too early for a Sitefinity application as it has not yet initialized all of its components, which happens on Bootstrapping, as Sitefinity uses an approach similar to the Composite Application Library Bootstrapper. Sitefintiy's BootStrapper can be hooked to on both its Initializing and Initialized events, in which we're both interested today.

The actual Routes registration in Sitefinity happens inside our implementation of the RegisterRoutes method of the above discussed Telerik.Sitefinity.Abstractions.Bootstrapper class. When you hook to the  Bootstrapper.Initialized event you have to filter by the RegisterRoutes CommandName argument of the event, to be able to plug your logic after the execution of our Telerik.Sitefinity.Abstractions.Bootstrapper.RegisterRoutes logic. To put it more clearly - by subscribing to:

public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
       }
 
       void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
       {          
           if (e.CommandName == "RegisterRoutes")
           {
               //here you can register your custom routes
           }
       }

you get access to an extensibility point of the system where Sitefinity has reached the point where it has registered its routes, so your route will be placed after the default Sitefinity routes in the RouteTable.Routes collection.

For the sake of today's post let's imagine we want our site visitors to navigate to http://yoursitedomain/yourcustomroute and actually end up on a physical (*.aspx) page form your application. To achieve this you first need to create the physical WebForm in your Sitefinity project. For the current example we've create done called CustomPage.aspx and placed in the project's root (~/CustomPage.aspx).

Now without routing your custom page can be accessed only through the http://yoursitedomain/custompage.aspx URL

However if you want http://yoursitedomain/customroute to open the CustomPage.aspx you'll need somethign like:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
        }
 
        void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName == "RegisterRoutes")
            {
                //register  route to the physical page ~/CustomPage.aspx
                System.Web.Routing.RouteTable.Routes.MapPageRoute("CustomPageRoute", "customroute", "~/CustomPage.aspx");
            }
        }

where we're directly registering our new route in the RouteTable.Routes collection just as in the standard ASP.NET approach, with the only difference being the time of the registration is done in Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized during the RegisterRoutes command.

 Please keep in mind that the default Sitefinity routes will always be loaded before your custom route, thus they will take precedence over yours (for example if we had a Sitefinity page called customroute, when requesting http://yoursitedomain/customroute Sitefinity will serve the Sitefinity page instead of CustomPage.aspx since the Sitefinity Frontend pages route is registered with precedence in the RouteTable). Another thing worth pointing out, if you are using Sitefinity Multisite the custom route will be resolved in all sites, and the above described scenario (Sitefinity page with the same URL taking precedence) will be valid only if you have such a page in the current site.

One last note we'd like to make is that with the above sample we're just scratching the surface of who your can utilize routing in your Sitefinity site, by demoing the very basics of ASP.NET Routing and showing the Sitefinity specifics when using it. We hope you enjoyed it, and found the information useful. As always, any comments and suggestions are more than welcome.

Leave a comment