Does page reload ever cause post?

不打扰是莪最后的温柔 提交于 2019-11-26 05:56:50

问题


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!


回答1:


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.




回答2:


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.




回答3:


A way to handle this is using tokens.

  1. Send a random string along with the post data
  2. Store this random string somewhere
  3. When you check the post data make sure that the stored string is the same as the string in the post request.
  4. If true, handle the request.
  5. 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

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