zoukankan      html  css  js  c++  java
  • 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)].

  • 相关阅读:
    iOS-导航条
    iOS-存储
    iOS-模型传递
    iOS-日期相关
    iOS-UIViewController
    iOS-loadView方法
    iOS-UIWindow
    Spring 测试
    Spring条件注解@Conditional
    Spring多线程
  • 原文地址:https://www.cnblogs.com/chucklu/p/15386103.html
Copyright © 2011-2022 走看看