ASP.NET Core MVC Interview Question #27

What is the purpose of the [HttpGet] attribute?

Controllers, Actions, Model Binding & Action Results Junior Beginner

Quick Interview Answer

HttpGet restricts an action to HTTP GET requests. GET actions are normally used to retrieve or display information and should not perform state-changing operations.

Detailed Explanation

HttpGet restricts an action to HTTP GET requests. GET actions are normally used to retrieve or display information and should not perform state-changing operations.

A common MVC pattern uses a GET action to display a form and a POST action to process the submitted form.

Code Example

[HttpGet]
public IActionResult Create()
{
    return View();
}