Detect if action is a POST or GET method

前端 未结 4 1256
孤街浪徒
孤街浪徒 2020-12-14 00:01

In MVC 3, is it possible to determine if an action is a result of a POST or GET method? I know you can decorate the actions with [HttpPost] and [HttpGet] to fire a specific

4条回答
  •  庸人自扰
    2020-12-14 00:32

    The HttpMethod property on the HttpRequest object will get it for you. You can just use:

    if (HttpContext.Current.Request.HttpMethod == "POST")
    {
        // The action is a POST.
    }
    

    Or you can get the Request object straight off of the current controller. It's just a property.

提交回复
热议问题