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

Forums / General Discussions / Routing and Redirecting in Sitefinity

Routing and Redirecting in Sitefinity

2 posts, 1 answered
  1. MacKenzie
    MacKenzie avatar
    2 posts
    Registered:
    17 Jan 2013
    12 Jul 2013
    Link to this post
    Hello all
     
     I have a need to have a kind of routing/redirecting page where I can look at the querystring and based off of what is in there route the user to another page. So for example if I had a query string like this:
     
     mysite.com/Home/johnsmith
     
    I would have the Home page be my redirection page and based off of the value of "johnsmith" I would then redirect the user to John Smiths page.
     
    In asp.net mvc this is very easy to do. However I am just getting my feet wet with Sitefinity so I have no idea how or if this can be done. I've Googled it quite a bit and I don't think I am asking the question correctly since I can't find anything.
     
    Any help would be much appreciated. Thanks in advance!
     -MacKenzie
  2. vitako
    vitako avatar
    22 posts
    Registered:
    28 Feb 2010
    16 Jul 2013 in reply to MacKenzie
    Link to this post
    Hi
    It is acctually pretty easy to do.
    1) Create a page in sitefinity, in you example it will be called home page.
    2) Create a user control, register it in sitefinity and drop it on the home page.
    3) In your user control you can capture the parameters the following way:

    Include the following namespace:
    using Telerik.Sitefinity.Web;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            var parms = (string[])this.Page.Request.RequestContext.RouteData.Values["Params"];
      
            if (parms != null)
                {
                string name = parms[0];
      
                if(name == 'johnsmith')
                {
                     //  This will tell sitefinity that parameters resolved
                            //  If this is not called, then you will get a 404 error
                            RouteHelper.SetUrlParametersResolved(true);
      
                    Response.Redirect("johnsmithpageurl");
                }
            }
        }
    }

    In this url mysite.com/Home/johnsmith. johnsmith will become a parameter
    You can send multiple parameters, for example mysite.com/Home/john/smith will send two parameters to mysite.com/Home page
    You can loop through those parameters:

    for (int i = 0; i < parms.Length; i++)
    {
        switch(params[i])
        {
            case "johnsmith":
                i++;
                //  Get Value
                valueVariable = parms[i];
                break;
            case "othername":
                i++;
                //  Get Value
                valueVariable = parms[i];
                break;
        }
    }

    Or read them the following way:
    string firstName = parms[0];
    string lastName = parms[1];

    Make sure to call RouteHelper.SetUrlParametersResolved(true);
    if you don't call this method then you will receive 404 error
    Regards,
    Answered
2 posts, 1 answered