When declaring a function with the HttpPost attribute what am I actually doing? Posting to the web server or returning data back to view?
Neither. When you declare an action method with this attribute:
[HttpPost]
public ActionResult SomeMethod(SomeModel model)
{
// code
}
What you're doing is indicating that requests should route to this method only if the request uses the POST
HTTP verb. So users can't attempt to invoke this action by issuing a GET
request, for example.
Whether or not HttpPost
is "good practice" is entirely subjective. Use it when you want to restrict the HTTP verb which can invoke that method, don't use it when you don't want to make that restriction.
ValidateAntiForgeryToken
is another attribute to use specifically when you want to enact a particular restriction. This works in conjunction with the @Html.AntiForgeryToken
form element helper. Its purpose is to ensure that requests which validate the token are received by users who have received a token, which helps prevent request forgeries.
Basically, if you place a @Html.AntiForgeryToken
in your view's form then it will create a unique value for that specific instance of that form. Then a form POST to any action method which validates the token will first check that the token is one that's been issued from the server before processing the request. That way users can't capture a form POST and replay it many times, because the server-issued token would need to be different each time.