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

Forums / General Discussions / How can i extend my own routes along side sitefinity 6

How can i extend my own routes along side sitefinity 6

2 posts, 0 answered
  1. Chris Hayward
    Chris Hayward avatar
    36 posts
    Registered:
    17 Dec 2009
    02 Jul 2013
    Link to this post
    hello,

    I have been trying to extend my own routes along side with sitefinity 6. I created a custom route handler but sitefinity gives a 404 error.

    What I am attempting to do is to reroute:
    "mydomain.com/myPage.aspx?id=34" to
    "mydomain.com/myPage/34"

    This is how my custom route handler looks like:

    namespace SitefinityWebApp
    {
        public class SiteRouteHandler : IRouteHandler 
        {
            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                return BuildManager.CreateInstanceFromVirtualPath("~/myPage.aspx", typeof(Page)) as IHttpHandler;
            }
        }
    }

    The Global.asax:

    void Application_Start(object sender, EventArgs e)
        {
             
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
        }

    void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs args)
        {
            if (args.CommandName == "RegisterRoutes")
            {
                var routes = ((EnumerableQuery<RouteBase>)args.Data).ToList();
                routes.Insert(0, new System.Web.Routing.Route("myPage" + "/{ID}", new SitefinityWebApp.SiteRouteHandler()));
            }
        }

     
    Any advice is appreciated 
  2. Jen Peleva
    Jen Peleva avatar
    568 posts
    Registered:
    10 Nov 2016
    05 Jul 2013
    Link to this post
    Hello Chris,

     Please check this forum thread, where the same problem is discussed:
    http://www.sitefinity.com/developer-network/forums/general-discussions-/httphandler-not-working#1501414
    First of all, you need to create a RouteHanlder, where you will call your HttpHandler:

    public class myRouteHandler : IRouteHandler
        {
            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                // Return your HttpHandler implementation
                return new MyHttpHandler();
            }
     
        }
    In the HttpHandler you can get the queryString parameter from the Request and if it matches the value you're looking for, redirect to some page with Response.Redirect:
    public class MyHttpHandler : IHttpHandler, IRequiresSessionState
    {
     
     
        public bool IsReusable
        {
            get { return false; }
        }
     
        public void ProcessRequest(HttpContext context)
        {
            var id = context.Request.QueryString["id"];
                 
            context.Response.Redirect("/test", true);
        }
    }
    Here's how to register the RouteHandler in the Global.asax:
    protected void Session_Start(object sender, EventArgs e)
    {
        Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
    }
     
    void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs args)
    {
        if (args.CommandName == "RegisterRoutes")
        {
            var routes = (System.Web.Routing.RouteCollection)args.Data;
            routes.Add("MyRoute", new System.Web.Routing.Route("/myPage.aspx", new myRouteHandler()));
     
        }
    }
    As for the 404 error you get, the most probable reason is that our default route handler handles the request first, so when adding your routehandler to the routes, you may want to add it with an index (use Insert).

    Regards,
    Jen Peleva
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered