Search code examples
c#asp.netquery-stringreturnurl

Specify returnURL In QueryString


This is probably a simple question but I am struggling with returnurl in a querystring. I know how to call the returnurl in a querystring into a Response.Redirect but I am not sure how to set the returnurl to a certain url. Can someone give me a example of how to do this?


Solution

  • I have a suggestion for you, I'm sure how much it is apt for your situation.

    Let me define a Static Dictionary<string,string> to save some key and corresponding URLs. Since it is statically defined you can access it from all other pages, this variable will get application scope. ie.,

    public static Dictionary<string, string> URLDictonary = new Dictionary<string, string>()
                                             {
                                              {"google","http://google.com/"}, 
                                              {"dotnet","http://www.dotnetperls.com/"},     
                                              {"querystring","http://www.dotnetperls.com/querystring"}
                                             };
    

    So that you can attach the key name with the URL as query string. It may look like the following:

    Response.Redirect("~/Somepage.aspx?returnURL=google");
    // Which means you are passing the key as query string
    

    Now you can get this key in sample page and redirect to the specific page based on the key as follows:

    string returnURL = Request.QueryString["returnURL"];
    if (returnURL != null)
    {
        Response.Redirect(URLDictonary[returnURL]);
    }
    

    Since we are passing google it will redirect to the corresponding value ie. "http://google.com/".

    Note : You can create similar Dictionary with your own keys and Urls. If it is defined in a different class then use class_name.DictonaryName[querystring_value]