Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC [HttpPost/HttpGet] for Action

I am using MVC C#.

Can somebody give an example on why one would use

[HttpPost/HttpGet]  

for an Action. How can an active have both - what is the practical use?

like image 890
Nate Pet Avatar asked Aug 01 '12 21:08

Nate Pet


People also ask

What is the HttpPost action verb in MVC?

HTTP Post. This verb is used while we have to create a new resource in the database. In HttpPost, data travels in the URL and body. To use HttpPost method, we use HttpPost attribute on the Action method.

How do you restrict access to action in MVC?

To restrict the public action method in MVC, we can use the “NonAction” attribute. The “NonAction” attribute exists in the “System. Web.

What is HttpGet HttpGet HttpPut HttpDelete and HttpPost?

The MVC framework includes HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions, and HttpPatch action verbs. You can apply one or more action verbs to an action method to handle different HTTP requests. If you don't apply any action verbs to an action method, then it will handle HttpGet request by default.

How redirect to action in view in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.


2 Answers

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)].

like image 107
Jesse Hallam Avatar answered Sep 19 '22 13:09

Jesse Hallam


You don't need to specify both at the same time, unless you're specifically restricting the other verbs (i.e. you don't want PUT or DELETE, etc).

Contrary to some of the comments, I was also unable to use both Attributes [HttpGet, HttpPost] at the same time, but was able to specify both verbs instead.

Actions

    private ActionResult testResult(int id)     {         return Json(new {                             // user input                             input = id,                             // just so there's different content in the response                             when = DateTime.Now,                             // type of request                             req = this.Request.HttpMethod,                             // differentiate calls in response, for matching up                             call = new StackTrace().GetFrame(1).GetMethod().Name                         },                         JsonRequestBehavior.AllowGet);     }     public ActionResult Test(int id)     {         return testResult(id);     }     [HttpGet]     public ActionResult TestGetOnly(int id)     {         return testResult(id);     }     [HttpPost]     public ActionResult TestPostOnly(int id)     {         return testResult(id);     }     [HttpPost, HttpGet]     public ActionResult TestBoth(int id)     {         return testResult(id);     }     [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]     public ActionResult TestVerbs(int id)     {         return testResult(id);     } 

Results

via POSTMAN, formatting by markdowntables

| Method    | URL                   | Response                                                                                  | |--------   |---------------------- |----------------------------------------------------------------------------------------   | | GET       | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041216116)/", "req": "GET", "call": "Test" }             | | POST      | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041227561)/", "req": "POST", "call": "Test" }            | | PUT       | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041252646)/", "req": "PUT", "call": "Test" }             | | GET       | /ctrl/testgetonly/5   | { "input": 5, "when": "/Date(1408041335907)/", "req": "GET", "call": "TestGetOnly" }      | | POST      | /ctrl/testgetonly/5   | 404                                                                                       | | PUT       | /ctrl/testgetonly/5   | 404                                                                                       | | GET       | /ctrl/TestPostOnly/5  | 404                                                                                       | | POST      | /ctrl/TestPostOnly/5  | { "input": 5, "when": "/Date(1408041464096)/", "req": "POST", "call": "TestPostOnly" }    | | PUT       | /ctrl/TestPostOnly/5  | 404                                                                                       | | GET       | /ctrl/TestBoth/5      | 404                                                                                       | | POST      | /ctrl/TestBoth/5      | 404                                                                                       | | PUT       | /ctrl/TestBoth/5      | 404                                                                                       | | GET       | /ctrl/TestVerbs/5     | { "input": 5, "when": "/Date(1408041709606)/", "req": "GET", "call": "TestVerbs" }        | | POST      | /ctrl/TestVerbs/5     | { "input": 5, "when": "/Date(1408041831549)/", "req": "POST", "call": "TestVerbs" }       | | PUT       | /ctrl/TestVerbs/5     | 404                                                                                       | 
like image 29
drzaus Avatar answered Sep 19 '22 13:09

drzaus