2

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?

5
  • Something like this Commented Jun 23, 2016 at 14:17
  • 1
    Little bit confusing, could you please be more specific Commented Jun 23, 2016 at 14:18
  • It would be weird that you would want to specify the return url in the backend, when it is the backend's job to respond to that return url request.... Commented Jun 23, 2016 at 14:19
  • 1
    Hi Jamie, Can you provide bit more context. Like any code examples, and also framework? Are you using MVC, Asp Core, Web forms? Commented Jun 23, 2016 at 14:28
  • I have added an answer, based on what i understand from the question. Please take a look Commented Jun 23, 2016 at 14:39

2 Answers 2

1

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]

Sign up to request clarification or add additional context in comments.

Comments

1

You could do it in the following:

var url = Request.Url.ToString();
var uri = String.Format("http://example.com?page={0}", url);
Response.Redirect(uri);

The code is pretty straight forward.

1 Comment

You will want to encode url before doing this to make sure that special characters are dealt with correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.