+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / ASP.NET URL Routing with Sitefinity?

ASP.NET URL Routing with Sitefinity?

9 posts, 0 answered
  1. Chris Johannsen
    Chris Johannsen avatar
    6 posts
    Registered:
    13 Aug 2009
    20 Aug 2009
    Link to this post
    I have been trying to setup URL Routing with a Sitefinity installation in order to create an easy way to use search friendly URLs... (e.g. http://localhost/Example/). Is this possible?
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    24 Aug 2009
    Link to this post
    Hello Chris Johannsen,

    Yes, this is possible.

    Sitefinity has a url rewriter built in. The first thing you could check is the UrlRewriteFormat defined for each module in the web.config file. You can use any format here by including any of the metafields in the property value. Further more you can eanble the Advance URL Rewriter and define your own rules. Please take a look in the User Manual, section URL Rewrite.

    Let me know if you need any further help.

    Best wishes,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  3. tdekoekkoek
    tdekoekkoek avatar
    13 posts
    Registered:
    09 Nov 2006
    24 Aug 2009
    Link to this post
    I believe what Chris wanted to do was to use .NET 3.5 routing not URL rewriting.  Chris this is possible, but getting the exact routing depends on what you want your URLs to look like.  I will be doing a similar thing when merging my MVC application with sitefinity, so I will be curious to know your progress.  I will let you know mine.

    -Trevor
  4. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    25 Aug 2009
    Link to this post
    Hi tdekoekkoek,

    I am also very interested in your progress. Please let us know how the things are going.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  5. Chris Johannsen
    Chris Johannsen avatar
    6 posts
    Registered:
    13 Aug 2009
    25 Aug 2009
    Link to this post
    Yes, I am wanting to use .Net 3.5 URL Routing to route URL's to aspx pages. For instance I have a url like http://localhost/Products/ and I want it to load the /products/default.aspx without changing the URL.
  6. Keith Lusk
    Keith Lusk avatar
    3 posts
    Registered:
    21 Sep 2009
    13 Nov 2009
    Link to this post
    Hi Guys,

    We got pretty far with the standard routing with SiteFinity. Our snag comes in after we complete our processing of the  requestContext.RouteData.Values. Our class inherits from the System.Web.Routing.IRouteHandler which requires the IHttpHandler return from the GetHttpHandler method. Usually this is accomplished with the following line of code where we are creating a page instance from a physical page on disk using .Net's BuildManager:
    return
    BuildManager.CreateInstanceFromVirtualPath( PageUrlString, typeof(Page)) as Page;

    I will supply the instructions to get to this point, but I am not certain how to Get this page instance from Sitefinities virtual pages.
    1. Add the following code to Global.Asax

     

    void Application_Start(object sender, EventArgs e)   
    {  
        // Code that runs on application startup   
        RegisterRoutes(RouteTable.Routes);    
    }  

     

    --Also in Global.Asax add a route.Add for each pattern you need to trap
     

    public static void RegisterRoutes(RouteCollection routes)   
    {  
        routes.Add(  
            "MyRouteName", new Route   
        ("Path/{Category1}/{Category2}/{Action}.Aspx", new MyRouteHandlerClass() //In App Code   
     ));  

     

    2. Add a Class to App Code:

    public class MyRouteHandlerClass: System.Web.Routing.IRouteHandler   
    {  
        //Constructor  
        public MyRouteHandlerClass(){}  
     
    //Add the Handler  
    public IHttpHandler GetHttpHandler(RequestContext requestContext)   
    {  
        string myAction = requestContext.RouteData.Values["Action"];  
        string myCategory1 =     requestContext.RouteData.Values["Category1"];  
        string myCategory2 =     requestContext.RouteData.Values["Category2"];  
        ...Your Code  
        ..If you need to clear the requestContext.routeData values and assign your own  
        //requestContext.RouteData.Values.Clear();   
        //requestContext.RouteData.Values.Add("Key", stringValue);   
     
    =====HERES THE PROBLEM=====   
    if(MyAction.ToLower() == "action1")   
    {  
    return BuildManager.CreateInstanceFromVirtualPath( "relativePath", typeof(Page)) as Page; //Needs to be an actual page  
    }   
    else if (MyAction.ToLower() == "action2")   
    {   
    return BuildManager.CreateInstanceFromVirtualPath( "relativePath", typeof(Page)) as Page;  
    }  


    To Use the routing data in a control or page it is available from the current request context 

    Must also add the following to Web.Config 

    In Assemblies:
    <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  

    In HttpModules:
    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
     

  7. Øyvind Mjølund
    Øyvind Mjølund avatar
    1 posts
    Registered:
    31 Jul 2009
    12 Feb 2010
    Link to this post
    Hi Keith,

    I don't know wether you managed to slove your problem or not, but I thought I'd post the solution I found (after some hours of looking through Sitefinity docs and dlls) as I had the same issue.

    The solution is actually to point the "virtualPath" parameter of the BuildManager.CreateInstanceFromVirtualPath method to cmsentrypoint.aspx in your web site's Sitefinity folder. And in addition to that you need to tell Sitefinity which page you wish to get. This is done by adding an item of type Telerik.Cms.Web.ICmsUrlContext to the HttpContext as cmsentrypoint.aspx looks for this item as it processes the page.

    Check out this example for the RegisterRoutes method in global.asax.
    (Only real difference is that the MyRouteHandler class takes a Guid instead of a string in its contructor)
    1.public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    2.{
    3.    Guid myPageGuid = new Guid("2039242A-622C-4829-A84C-A01417A1450D");        
    4.    routes.Add("MyRoute", new System.Web.Routing.Route(
    5.                                  "MyRoute/{Controller}/{Action}"
    6.                                   new Custom.Handlers.MyRouteHandler(myPageGuid)));
    7.}

    Then the route handler:
    (Add a reference (and using clause) for System.Web.Abstractions and Telerik.Cms)
    01.public class MyRouteHandler: IRouteHandler
    02.    {
    03.        private const string CMS_ENTRYPOINT_PATH = "~/Sitefinity/cmsentrypoint.aspx";
    04.  
    05.        public Guid PageGuid { get; set; }
    06.  
    07.        public CalendarRouteHandler(Guid pageGuid)
    08.        {
    09.            this.PageGuid = pageGuid;
    10.        }
    11.  
    12.        #region IRouteHandler Members
    13.          
    16.        public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
    17.        {
    18.            string controller = (string)requestContext.RouteData.Values["Controller"];
    19.            string action = (string)requestContext.RouteData.Values["Action"];
    20.  
    21.            //use UrlHelper to get Url by Guid and add to cmsurlcontext
    22.            requestContext.HttpContext.Items.Add("cmsurlcontext", Telerik.Cms.Web.UrlHelper.GetUrl(new Guid()));
    23.             
    24.            var page = BuildManager.CreateInstanceFromVirtualPath(CMS_ENTRYPOINT_PATH, typeof(System.Web.UI.Page)) as IHttpHandler;
    25.            return page;
    26.        }
    27.         #endregion
    29.    }

    - Øyvind
  8. Keith Lusk
    Keith Lusk avatar
    3 posts
    Registered:
    21 Sep 2009
    22 Jun 2010
    Link to this post
    Thanks Øyvind. I never did get this resolved and ended up using the sitefinity routing instead. I hope I have an opportunity to try this on another project. Thanks again.
  9. Joe Groner
    Joe Groner avatar
    2 posts
    Registered:
    26 Oct 2009
    16 Dec 2010
    Link to this post
    public class SitefinityRouteHandler : IRouteHandler
        {
            private string pageUrl;
     
            public SitefinityRouteHandler(string pageUrl)
            {
                this.pageUrl = pageUrl;
            }
     
            #region IRouteHandler Members
     
            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                string cmsEntryPointPath = "~" + Telerik.Cms.Web.UrlHelper.PublicEntryPoint;
                string page = pageUrl.StartsWith("~") ? pageUrl.Substring(1) : pageUrl;
                requestContext.HttpContext.Items.Add("cmsurlcontext", Telerik.Cms.Web.UrlHelper.GetUrl(page));
                return BuildManager.CreateInstanceFromVirtualPath(cmsEntryPointPath, typeof(Page)) as IHttpHandler;
            }
Register for webinar
9 posts, 0 answered