<p>I am working on a project management web application.  The user has a variety of ways to display a list of tasks.  When viewing a list page, they click on task and are redirected to the task edit page.  </p>

<p>Since they are coming from a variety of ways, I am just curious as to the <strong>best</strong> way to <strong>redirect</strong> the user back to the calling page.  I have some ideas, but would like to get other developers input.</p>

<p>Would you store the <strong>calling</strong> url in session? as a cookie?  I like the concept of using an object <strong>handle</strong> the redirection.</p>

[{'answer_id': 36735, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 1, 'selected': False, 'text': '<p>I personally would store the required redirection info in an object and handle globally. I would avoid using a QueryString param or the like since they could try bouncing themselves back to a page they are not supposed to (possible security issue?). You could then create a static method to handle the redirection object, which could read the information and act accordingly. This encapsulates your redirection process within one page.</p>\n\n<p>Using an object also means you can later extend it if required (such as adding return messages and other info).</p>\n\n<p>For example (this is a 2 minute rough guideline BTW!):</p>\n\n<pre><code>public partial class _Default : System.Web.UI.Page \n{\n\n    void Redirect(string url, string messsage)\n    {\n        RedirectionParams paras = new RedirectionParams(url, messsage);\n        RedirectionHandler(paras); // pass to some global method (or this could BE the global method)\n    }\n    protected void Button1_Click(object sender, EventArgs e)\n    {\n        Redirect("mypage.aspx", "you have been redirected");\n    }\n}\n\npublic class RedirectionParams\n{\n    private string _url;\n\n    public string URL\n    {\n        get { return _url; }\n        set { _url = value; }\n    }\n\n    private string _message;\n\n    public string Message\n    {\n        get { return _message; }\n        set { _message = value; }\n    }\n\n    public RedirectionParams(string url, string message)\n    {\n        this.URL = url;\n        this.Message = message;\n    }\n}\n</code></pre>\n'}, {'answer_id': 36777, 'author': 'Tom', 'author_id': 3139, 'author_profile': 'https://Stackoverflow.com/users/3139', 'pm_score': 4, 'selected': True, 'text': '<p>I would store the referring URL using the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate.aspx" rel="noreferrer">ViewState</a>. Storing this outside the scope of the page (i.e. in the Session state or cookie) may cause problems if more than one browser window is open.</p>\n\n<p>The example below validates that the page was called internally (i.e. not requested directly) and bounces back to the referring page after the user submits their response.</p>\n\n<pre><code>public partial class _Default : System.Web.UI.Page\n{\n    protected void Page_Load(object sender, EventArgs e)\n    {\n        if (Request.UrlReferrer == null)\n        {\n            //Handle the case where the page is requested directly\n            throw new Exception("This page has been called without a referring page");\n        }\n\n        if (!IsPostBack)\n        {   \n            ReturnUrl = Request.UrlReferrer.PathAndQuery;\n        }\n    }\n\n    public string ReturnUrl\n    {\n        get { return ViewState["returnUrl"].ToString();  }\n        set { ViewState["returnUrl"] = value; }\n    }\n\n    protected void btn_Click(object sender, EventArgs e)\n    {\n        //Do what you need to do to save the page\n        //...\n\n        //Go back to calling page\n        Response.Redirect(ReturnUrl, true);\n    }\n}\n</code></pre>\n'}, {'answer_id': 36781, 'author': 'Robin Barnes', 'author_id': 1349865, 'author_profile': 'https://Stackoverflow.com/users/1349865', 'pm_score': 1, 'selected': False, 'text': '<p>This message my be tagged asp.net but I think it is a platform independent issue that pains all new web developers as they seek a \'clean\' way to do this.</p>\n\n<p>I think the two options in achieving this are:</p>\n\n<ol>\n<li>A param in the url</li>\n<li>A url stored in the session</li>\n</ol>\n\n<p>I don\'t like the url method, it is a bit messy, and you have to remember to include the param in every relevent URL.</p>\n\n<p>I\'d just use an object with static methods for this. The object would wrap around the session item you use to store redirect URLS.</p>\n\n<p>The methods would probably be as follows (all public static):</p>\n\n<ul>\n<li>setRedirectUrl(string URL)</li>\n<li>doRedirect(string defaultURL)</li>\n</ul>\n\n<p>setRedirectUrl would be called in any action that produces links / forms which need to redirect to a given url. So say you had a projects view action that generates a list of projects, each with tasks that can be performed on them (e.g. delete, edit) you would call RedirectClass.setRedirectUrl("/project/view-all") in the code for this action.</p>\n\n<p>Then lets say the user clicks delete, they need to be redirected to the view page after a delete action, so in the delete action you would call RedirectClass.setRedirectUrl("/project/view-all"). This method would look to see if the redirect variable was set in the session. If so redirect to that URL. If not, redirect to the default url (the string passed to the setRedirectUrl method).</p>\n'}, {'answer_id': 36797, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with "rmbarnes.myopenid.com" regarding this issue as being platform independent.</p>\n\n<p>I would store the calling page URL in the QueryString or in a hidden field (for example in ViewState for ASP.NET). If you will store it outside of the page scope (such as Session, global variable - Application State  and so on) then it will not be just overkill as Tom said but it will bring you trouble.</p>\n\n<p>What kind of trouble? Trouble if the user has more than one tab (window) of that browser open. The tabs (or windows) of the same browser will probably share the same session and the redirection will not be the one expected and all the user will feel is that it is a bug.</p>\n\n<p>My 2 eurocents..</p>\n'}]
['https://Stackoverflow.com/questions/36733', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1768/']