Quick question, I have tried figuring this out myself, but the use of Session Variables can be confusing when trying to figure out why or how a page is reloading and doing/not doing what it is supposed to do.
Does a page reload (with JavaScript, f5, ctrl+f5, browser reload button, etc) ever, under any (non-scripted) circumstance cause a form repost?
(This pertains to using IfPost branches within C# code like the example code below):
if(IsPost)
{
//stuff that only executes if the previous request was a post.
}
I just kind of need to know what to expect here so I can properly diagnose my session state problems.
Thanks for any help!
(Also, sorry if I am oversimplifying this question. I realize that it 'may' be more complicated than a simple answer can provide).
******UPDATE********** Also, I looked for copies of this question here, on StackOverflow, but I didn't see anything, so if this is a duplicate question, I apologize.
Both of the answers here are good answers. I accepted the answer that also offered a solution to my question, and while PRG is not the 'only' way to do it, it seems like it may be the best. It also seems a growing way to handle client-side user-friendliness, and imho, seems like it would be a great habit to get into.
Thanks for showing me that!
Yes. If the page was loaded using POST data this will occur. To prevent this you need to implement the POST/REDIRECT/GET
pattern.
Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG supports bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.
Yes. If someone refreshes the browser manually, it will ask them if they want to send the form data again. This will cause that code to get executed.
A way to handle this is using tokens.
- Send a random string along with the post data
- Store this random string somewhere
- When you check the post data make sure that the stored string is the same as the string in the post request.
- If true, handle the request.
- Generate a new token
If someone were to refresh and resend the post data, your token in the post request will be different from the one you stored separately since you generated a new token at step 5
来源:https://stackoverflow.com/questions/13883917/does-page-reload-ever-cause-post