Note MVC [HttpGet] controller optional?

If I have two controller actions:

[HttpGet] public ActionResult Login() { //... return View(); } 

and

  [HttpPost] public ActionResult Login(FormCollection values) { //... return RedirectToAction("Index","Home"); } 

This seems to require Post design (this makes sense), but HttpGet decoration is completely optional. It works great with or without. MVC seems to control the behavior of the HttpGet controller by default, unless otherwise specified.

I need to decide if I want the future reader of my code to have to figure it out on my own or not, or if I want to forget to add HttpGet everywhere for consistency. But my question is not whether it is good practice to include an explicit decoration, even if it is not already made.

My question is: ALWAYS it happens that I don’t need to decorate HttpGet controller methods? Is there any way that this can bite me if I do this or explicitly do not indicate? I searched for this, but all I can find are posts describing why you can use both annotations and not the reason / cons, including HttpGet.

+7
asp.net-mvc asp.net-mvc-controller
source share
1 answer

You do not need to state this explicitly, no. However, please note:

  • Not specifying an action verb will mean that the method accepts both GET and POST. However, if there are two actions, then POST will be used for POST, and the other will be used by default for GET.
  • Using HttpGet will mean that the action accepts only GET requests.
  • Labeling actions like GET may make what you intend more obvious to other developers.

Is there a way that this can bite me if I do this or do not state explicitly?

Not very likely. I could imagine a situation where something might seem like weird behavior or doesn't work properly because of this, but that would be rare.

+10
source

All Articles