Kjensen
Kjensen

Reputation: 12374

How can I check if a controller was called using post or get?

In ASP.Net MVC, how can I check in a controller method, if it was called using post or get?

I am aware I can limit methods to being called only by post or by get - but how can I check for post/get in a method that allows both?

Upvotes: 20

Views: 12017

Answers (3)

Arun Prasad E S
Arun Prasad E S

Reputation: 10115

I use this combination for checking in view also

var method = Request.HttpMethod;

if (Request.HttpMethod == "POST") {
    var check = "";
}

if (Request.HttpMethod == "GET") {
    var check = "";
}

Upvotes: 0

LukLed
LukLed

Reputation: 31842

ControllerContext.HttpContext.Request.HttpMethod

or just

Request.HttpMethod 

in Controller

Upvotes: 38

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

You can check the Request.HttpMethod property.

Upvotes: 11

Related Questions