It's accessible via any verb.
Let's say you have a Login
action which provides the user with a login screen, then receives the user name and password back after the user submits the form:
public ActionResult Login() {
return View();
}
public ActionResult Login(string userName, string password) {
// do login stuff
return View();
}
MVC isn't being given clear instructions on which action is which, even though we can tell by looking at it. If you add [HttpGet] to the first action and [HttpPost] to the section action, MVC clearly knows which action is which.
Why? See Request Methods. Long and short: When a user views a page, that's a GET request and when a user submits a form, that's usually a POST request. HttpGet and HttpPost just restrict the action to the applicable request type.
[HttpGet]
public ActionResult Login() {
return View();
}
[HttpPost]
public ActionResult Login(string userName, string password) {
// do login stuff
return View();
}
You can also combine the request method attributes if your action serves requests from multiple verbs:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
.
[HttpPost]
, then the one without an attribute is implicitly GET only.ASP .NET MVC NonAction meaning
You can omit the
NonAction
attribute but then the method is still invokable as action method.From the MSDN site (ref):
What is the default behaviour of a controller action not marked with AcceptVerbs, HttpGet or HttpPost?
It's accessible via any verb.
MVC [HttpPost/HttpGet] for Action
Let's say you have a
Login
action which provides the user with a login screen, then receives the user name and password back after the user submits the form:MVC isn't being given clear instructions on which action is which, even though we can tell by looking at it. If you add [HttpGet] to the first action and [HttpPost] to the section action, MVC clearly knows which action is which.
Why? See Request Methods. Long and short: When a user views a page, that's a GET request and when a user submits a form, that's usually a POST request. HttpGet and HttpPost just restrict the action to the applicable request type.
You can also combine the request method attributes if your action serves requests from multiple verbs:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
.