asp.net - 将用户从编辑页面重定向回调用页面

标签 asp.net redirect

我正在开发一个项目管理 Web 应用程序。用户有多种方式来显示任务列表。查看列表页面时,他们单击任务并重定向到任务编辑页面。

由于它们来自多种方式,我只是对感到好奇。最好的方式 重定向 用户返回调用页面。我有一些想法,但希望得到其他开发人员的意见。

你会储存吗?调用 session 中的网址?作为 cookies ?我喜欢使用对象的概念 handle 重定向。

最佳答案

我将使用 ViewState 存储引用 URL .如果打开多个浏览器窗口,将其存储在页面范围之外(即在 session 状态或 cookie 中)可能会导致问题。

下面的示例验证页面被内部调用(即不是直接请求)并在用户提交响应后返回到引用页面。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.UrlReferrer == null)
        {
            //Handle the case where the page is requested directly
            throw new Exception("This page has been called without a referring page");
        }

        if (!IsPostBack)
        {   
            ReturnUrl = Request.UrlReferrer.PathAndQuery;
        }
    }

    public string ReturnUrl
    {
        get { return ViewState["returnUrl"].ToString();  }
        set { ViewState["returnUrl"] = value; }
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        //Do what you need to do to save the page
        //...

        //Go back to calling page
        Response.Redirect(ReturnUrl, true);
    }
}

关于asp.net - 将用户从编辑页面重定向回调用页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733/

相关文章:

c# - 使用 lambda 表达式更新 linq 中的多个列

c# - 如何从ASP.NET MVC 2中的Application_Error重定向?

php - .htaccess 结合 2 RewriteRule

http - 多次重定向

c# - 程序集绑定(bind)和重定向

PHP 将一个页面重定向到另一台服务器而不显示 URL

asp.net - 相当于 VB.NET 中的 SQL IN

c# - 使用 HTML5 视频标签通过 ashx 处理程序从 sql server blob 播放视频文件

c# - 获取错误 foreach statement cannot operate on variables of type '' because '' does not contain a public definition for 'GetEnumerator'

c# - 如何将对象列表存储到 ViewState