Post-Redirect-Get (PRG) Summary Page on Submit

一个人想着一个人 提交于 2019-12-23 20:24:10

问题


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.

Here is my PRG Code:

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 ;)


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/9775684/post-redirect-get-prg-summary-page-on-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!