If I do not specify [httpGet] or [httpPost] for the action method , what will be the rule for calling it

If I do not specify [httpGet] or [httpPost] for the action method , what will be the rule for calling it

What I understand, By default It accept both type of request whether it is GET or POST. but when a action method is decorated with either [httpGet] or [httpPost] attribute then the action method accepts only those request method which define by attribute.

评论:

Assuming of course that you don't add one and not the other. For example, if you have the same action name, one with no attribute and the other with [HttpPost], then the one without an attribute is implicitly GET only. Oct 23 '14 at 17:24
 
 

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):

By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.

 

 

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:

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)].

 

 

 

 

 

posted @ 2021-10-09 15:48  ChuckLu  阅读(39)  评论(0编辑  收藏  举报