提交后的重定向后获取(PRG)摘要页面 [英] Post-Redirect-Get (PRG) Summary Page on Submit

查看:120
本文介绍了提交后的重定向后获取(PRG)摘要页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我阅读了称为PRG的方法,以解决表单双重提交问题.但是,我还没有找到显示给用户的摘要页面/成功消息"的简化实现.我能想到的唯一方法是存储一个会话变量,但我不希望它在多次刷新后都保持不变.它应该只显示一次消息/摘要,然后完成.此外,如果用户无法返回到先前提交的页面,那将是理想的选择.

So I read up on this method called PRG as a way of addressing the form double-submit issue. However, I have yet to find a descent implementation of the Summary Page / Success Message displayed to the user. The only way I can think of is storing a session variable, but I don't want it to persist on multiple refreshes. It should show the message/summary once, and be done. Furthermore, it would be ideal if the user could not return to the previously submitted page.

这是我的PRG码:

Protected Sub InsertRequest() Handles wizard.FinishButtonClick
    Using connection As New SqlConnection(connectionStr)
        Dim insertQuery As New SqlCommand("spInsertRequest", connection)
        insertQuery.CommandType = CommandType.StoredProcedure

        '1 - SETUP SQL PARAMETERS (omitted for brevity)

        '2 - POST (inserts record into DB)
        Try
            connection.Open()
            insertQuery.ExecuteNonQuery()
            connection.Close()
        Catch ex As Exception
            Logger.WriteToErrorLog(Me, ex.Source, ex.Message, ex.StackTrace)
        End Try

    '3 - REDIRECT (to the same page and...)
    Try
        Dim urlRedirect As String = If(IsNothing(Request.Url), "", IO.Path.GetFileName(Request.Url.AbsolutePath)) 'Gets the filename of the current page.
        If Not String.IsNullOrEmpty(urlRedirect) Then
            Session.Add("referrerPage", urlRedirect) 'Used for identifying when the page is being redirected back to itself.
            PageExt.AddParam(urlRedirect, "id", recID.ToString)
            Response.Redirect(urlRedirect)
        End If
    Catch ex As Exception
        Logger.WriteToErrorLog(Me, ex.Source, ex.Message, ex.StackTrace)
    End Try
End Sub

'4 - GET (Display 'Success' message/summary here)

问题是,如何在重定向中直接显示由提交产生的​​消息,最好不要进行任何进一步的刷新?或者只是简单地显示消息,而不管刷新,最简单,最有意义的事情.谢谢;)

The question is, how to display this message on the redirect which directly results from the submit, and preferably not for any further refreshes? Or just simply display the message regardless of refreshes, whatever is easiest and makes the most sense. Thanks ;)

推荐答案

仅将这样的消息显示一次的技巧是使用闪存"会话数据的概念.

The trick to having messages like this displayed only once is to use the concept of "flash" session data.

这通常的工作方式是在重定向之前在会话中存储消息"(或与您所需的成功"相关的任何其他数据).然后,在处理重定向时,请确保在发送成功页面"响应之前,从会话中删除会话数据.

The way this usually works is to store the "message" (or whatever other data related to the "success" that you need) in the session prior to the redirect. Then, when processing the redirect, make sure to remove the flash data from the session prior to sending the "success page" response.

这样,如果用户尝试返回到成功页面,则闪存数据将不在要拾取的会话中,因此不会显示两次.为了使用户感到高兴,您可以检查Flash数据是否丢失,如果他们试图转到Post-Redirect-Get流程之外的成功页面,则可以显示一条错误消息.

This way, if the user tries to come back to the success page, the flash data will not be in the session to be picked up, so you won't have it displayed twice. To be nice to the users, you can check for the flash data to be missing and display a nice error message if they try to get to the success page outside of the Post-Redirect-Get flow.

这正是Grails框架(在Groovy世界中)所做的,并且效果很好.

This is exactly what the Grails framework (in the Groovy world) does and it works very well.

这篇关于提交后的重定向后获取(PRG)摘要页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆