What is the difference between Get and post method type in Controller in ASP.NET MVC

Posted by Rjain8 under ASP.NET on 1/20/2011 | Points: 10 | Views : 19001 | Status : [Member] | Replies : 3
Hi,

There are two type of methods in Controller in ASp.NET MVC: Get and HttpPost.
What is the difference between these two type of method?

Thanks,




Responses

Posted by: Karthikanbarasan on: 1/20/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
GET Method:

1.All the name value pairs are submitted as a query string in URL.
It's not secured as it is visible in plain text format in the Location bar of the web browser.

2.Length of the string is restricted.

3.If get method is used and if the page is refreshed it would not prompt before the request is submitted again.

4.One can store the name value pairs as bookmark and directly be used while sharing with others - example search results.


POST Method:

1. All the name value pairs are submitted in the Message Body of the request.

2. Length of the string (amount of data submitted) is not restricted.

3. Post Method is secured because Name-Value pairs cannot be seen in location bar of the web browser.

4. If post method is used and if the page is refreshed it would prompt before the request is resubmitted.

5. If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.

6. Data is submitted in the form as specified in enctype attribute of form tag and thus files can be used in FileUpload input box.


Thanks
Karthik
www.f5Debug.net

Rjain8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 1/20/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down

hi Rjain8
here the some differences between Get and post method
A : http Request AS GET may gets fails, if exceed certain
length in request URI as it is passed as query string
B : uploading files through input type file not possible
C : Chances for Data lost after server encoding

POST :
A : It does not fails in http Request AS POST
B : uploading files through input type file is possible
C : Data does not lost after server encoding

Hope fully it's your required full fill

thanks & regards
Prabhakar

Best Regard's
Prabhakar

Rjain8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Xanblax on: 3/10/2011 [Member] Starter | Points: 25

Up
0
Down
When you create a new Controller class on a ASP.NET MVC VS Project (right-clicking over folder "Controllers", then Add, then Controller), a dialog box lets you choose VS to autogenerate methods for Create, Update, Delete and Details scenarios.
When you do so, two methods are generated for each of these scenarios, for example:

//
// GET: /Address/Create
public ActionResult Create(int customerId) {...}

//
// POST: /Address/Create
[HttpPost]
public ActionResult Create(int customerId, FormCollection collection) {...}


And this can be confusing. One could think that only one function is necessary to "create" a "customer" (this is the example).
To understand why does VS generate two methods, we only have to add a breakpoint on each one of them and see when are they called:
The first one, the GET one, is called when the user clicks on the link that sais "create new customer" on the web page.
This "click" (the "get" method) should display a new page with the text boxes needed to fill in all the fields that a customer needs (name, company, address, and so on, whatever).
This page has an "OK" button at the bottom.
Only when this button is pressed the new customer will be really created and added to the database.
Well: This button is what triggers the second function, the "POST" one.
So this "post" method (the OK button) should insert the new customer into the database and then return back to the initial page or whatever.

So, you see that we need to call two functions in order to accomplish the creation, and each one of them is needed and have a very different aim.

You do not always need this double-step approach:
To delete a customer, for example, you will probably need to use only one function, the "get" method should be enough: You click on a "delete" link, then the function triggered should delete the item from the database and then return the same page. No need to have a form, to fill in fields...

In short:
The two-steps approach of the MVC Controller is intended for this:
- the MVC Controller GET method (click "create") returns the form page, with the input boxes, etc.
- then the MVC Controller POST method (click "OK") creates the new instance of the entity with the data that the form has, then inserts it into the database, and finally returns to the previous page.

I hope this helps!

Kind regards,

Ricardo.

Rjain8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response