4

因此,我阅读了这种称为 PRG 的方法,作为解决表单重复提交问题的一种方法。但是,我还没有找到向用户显示的摘要页面/成功消息的下降实现。我能想到的唯一方法是存储会话变量,但我不希望它在多次刷新时持续存在。它应该显示一次消息/摘要,然后完成。此外,如果用户不能返回到先前提交的页面,那将是理想的。

这是我的 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)

问题是,如何在直接由提交产生的​​重定向上显示此消息,最好不要进一步刷新?或者只是简单地显示消息而不考虑刷新,无论是最简单和最有意义的。谢谢 ;)

4

1 回答 1

3

让这样的消息只显示一次的诀窍是使用“闪存”会话数据的概念。

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

这样,如果用户尝试返回成功页面,flash 数据将不会在要提取的会话中,因此您不会显示两次。为了对用户好,您可以检查闪存数据是否丢失,并在他们尝试访问 Post-Redirect-Get 流程之外的成功页面时显示一个很好的错误消息。

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

于 2012-03-19T18:26:13.843 回答