Search code examples
asp.netpost-redirect-get

How do I implement the Post/Redirect/Get pattern in asp.net WebForms?


So I understand the basics of the prg pattern. But I haven't come across a technique for making arbitrary data available to the "get" instance of a page.

For example, I might want to display different feedback messages to the user depending on their action which initiated the PostBack.

What I've been doing is sending an identifier as a query string parameter. This works fine but it introduces bookmarking issues and doesn't seem like it would scale very well. What if I needed to send all of the ViewState?

Unfortunately, I'm tied to WebForms at the moment and haven't been able to convince my organization to migrate to mvc.


Solution

  • If the fields POSTed are being persisted before the Redirect and you need to access that data after the Redirect, I would append the identifier for the data record(s) on the query string as you mention. You could also specify a status for the request (for displaying messages etc). Then on the GET page you can read out the data and do whatever with it.

    I don't see any other way to get around this as each page obtained by GET will not have access to the previous page's ViewState etc.

    Using Server.Transfer will have the same effect as handling the POST on the original page.

    You could use Session variables to store the POST data, but that stinks.