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?
-
Something like thisCallum Linington– Callum Linington2016-06-23 14:17:48 +00:00Commented Jun 23, 2016 at 14:17
-
1Little bit confusing, could you please be more specificsujith karivelil– sujith karivelil2016-06-23 14:18:03 +00:00Commented 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....Callum Linington– Callum Linington2016-06-23 14:19:17 +00:00Commented Jun 23, 2016 at 14:19
-
1Hi Jamie, Can you provide bit more context. Like any code examples, and also framework? Are you using MVC, Asp Core, Web forms?Phillip Morton– Phillip Morton2016-06-23 14:28:57 +00:00Commented Jun 23, 2016 at 14:28
-
I have added an answer, based on what i understand from the question. Please take a looksujith karivelil– sujith karivelil2016-06-23 14:39:03 +00:00Commented Jun 23, 2016 at 14:39
2 Answers
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]
Comments
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.