Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display messages to the user after a POST + HTTP redirect

Tags:

I’m using the PRG pattern to avoid multiple form submission. It has, however, a serious drawback — you cannot simply echo the confirmation message to the user (obviously, the user won’t see the page, he will be redirected to another one).

What are the solutions to this problem? I know two of them, but none of them seems perfect.

  • Use a custom redirect URL, like: http://example.com/?msg=data-saved. It’s stateless, so I think it’s quite reliable. But it creates problems when user copies the link, bookmarks it, etc.
  • Store a session variable/cookie, and check it on every page load. If it’s set, clear it and display the message. It seems OK, but I’m not sure about this one — it relies strongly on cookies, it’s a little more complicated.

Or maybe there are other ways I don’t know about? Some combination of sessions and URL parameters? I don't know.

What’s the best way in your opinion? Which one has the least drawbacks? What are the pros and cons?

like image 551
Maciej Łebkowski Avatar asked Jun 29 '09 14:06

Maciej Łebkowski


1 Answers

There are several other stack overflow questions that touch on this, though I don't think any summarize the problem so clearly. Here are a few:

  • How do I implement the Post/Redirect/Get pattern in asp.net WebForms?
  • Response.Redirect with POST instead of Get?
  • Both HTTP GET with request body and Can an HTTP GET legally contain content? approach the issue, and the discussions are worth reading.
  • How do I maintain ModelState errors when using RedirectToAction?

Most of the convenient solutions are session-based or have more serious drawbacks (such as embedding the message in the querystring).

If you cannot guarantee that you'll have sessions, another (quite costly) method is to redirect to different views depending on the outcome of the form submission. For example, you might redirect to EditWidgetView, EditWidgetSaveSuccessfulView, or EditWidgetSaveErrorView (or perhaps you just don't redirect on errors). In some languages and frameworks, this is impractical to the point of making you give up on showing confirmation / error messages at all, but in others it may be worth it.

like image 66
Jeff Sternal Avatar answered Sep 28 '22 11:09

Jeff Sternal