程序go 2016-04-25 08:22 采纳率: 100%
浏览 40

ASP.NET防止浏览器“退回”

I'm trying to find a solution to a problem I'm facing with an ASP.NET application (C# / MySQL Backend).

The application uses webforms in a multi step wizard to insert data into a SQL database, the various steps are javascript driven so no 'submit' is done until the 'last' step. In certain circumstances anyway if the user click the browser 'back' button just after the 'last' step the form is resubmitted and the SQL INSERT is triggered again creating dupes of the original record.

I tought to use an Ajax based mechanism to track the steps of the form until the wizard reach the 'last' step. After that trough the same Ajax function I will prevent the resubmit of the form preventing dupes.

I don't want to use javascript tricks to disable the back button for obvious reasons.

The main problem of this solution is that I don't like it so much and it will also be quite difficult to implement since wizards on the application form are quite articulated and this may cause some problem.

Is there a 'best practice' to prevent such issues without building complex infrastructures?

I'm not posting code because I think it will be misleading and unnecessary.

Best regards, Mike

  • 写回答

1条回答 默认 最新

  • weixin_33712881 2016-05-06 15:43
    关注

    I found a solution to my problem. I think this can be a reasonably good practice for people facing the same problem. Of course it's not perfect but maybe someone else can refine this as the 'perfect solution'.

    First of all in the .aspx code of the page launching the form that was duplicating records I placed this:

    <asp:LinkButton
        ID="_btnForm1"
        runat="server"
        OnCommand="Link_Command"
        CommandArgument='<%#Eval("id","~/Form1.aspx?id={0}&step=Exit")%>'>
        Form1
    </asp:LinkButton>
    

    In the backend code I created an hanlder for the Link_Button command:

    protected void Link_Command(object sender, CommandEventArgs e)
    {
        Session["key"] = Guid.NewGuid().ToString().Replace("-", string.Empty);
    
        Response.Redirect(e.CommandArgument.ToString());
    }
    

    In short it generates an unique hash code storing it as a Session value.

    In the form managing records I checked for the Session["key"] value to be present and not null. If the key is present the code hasn't reached it's critical part then the forms should be resubmitted without problems.

    When the code reaches the critical part (just before data insertion into database) I cleared up the Session["key"] value:

    Session.Remove("key");
    

    Then if the user tries to resubmit the form after the critical part the Session key isn't present anymore and the code could trap the 'anomaly' and show up an error message or redirect over a different page:

    if (Session["key"] == null)
    {
          Response.Redirect("Error.aspx?script=" + HttpUtility.UrlEncode(Request.Path));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?