Home > ASP.NET MVC > ASP.NET # MVC # 9 – Difference between HTTPGet and HTTPPost with Example

ASP.NET # MVC # 9 – Difference between HTTPGet and HTTPPost with Example

Hi Geeks,

When we develop web applications we always have to deal with the HTML.

Today we will see the difference between the HTTPGet and HTTPPost attributes of ASP.NET MVC.

These attributes encode request parameters as name-and-value pairs in the HTTP request. The HTTP-GET protocol and the HTTP-POST protocol provide backward compatibility in the following ways.

 

* For example, you may want two versions of the Edit method, one that renders the edit form and the other that handles the request when that form is posted:

 

[HttpGet]
public ActionResult Edit(string id)
{
return View();
}
[HttpPost]
public ActionResult Edit(string id, FormCollection form)
{
//Save the item and redirect…
}

 

When a POST request for /home/edit is received, the action invoker creates a list of all methods of the Controller that match the edit action name. In this case, you would end up with a list of two methods.

Afterward, the invoker looks at all of the ActionSelectorAttribute instances applied to
each method and calls the IsValidForRequest method on each. If each attribute returns true, then the method is considered valid for the current action.

For example, in this case, when you ask the first method if it can handle a POST request, it will respond with false because it only handles GET requests.

The second method responds with true because it can handle the POST request, and it is the one selected to handle the action.

If no method is found that meets these criteria, the invoker will call the HandleUnknownAction method on the Controller, supplying the name of the missing action. If more than one action method meeting these criteria is found, an InvalidOperationException is thrown.

 

* Difference between HTTPGet and HTTPPost methods –

Fundamental Difference is probably the Visibility

The HTTPGet protocol creates a query string of the name-and-value pairs and then appends the query string to the URL of the script on the server that handles the request. Therefore, you can mark the request.

The HTTPPost protocol passes the name-and-value pairs in the body of the HTTP request message.

Length

Since, HTTPGet request goes via URL, so it has a limitation for its length. It can’t be more than 255 characters long (though this is browser dependent, but usually the max is 255 characters only).

Whereas

No such maximum length limitation holds for the HTTPPost request for the obvious reason that it becomes a part of the body of the HTTP request and there is no size limitation for the body of an HTTP request/response.

Performance

HTTPGet request is comparatively faster as it’s relatively simpler to create a GET request and

the time spent in the encapsulation of the HTTPPost request in the HTTP body is saved in this case. In addition, the maximum length restriction facilitates better optimization of HTTPGet implementation.

Type of Data

HTTPGet request is sent via URL string and as we all know that URL can be text-only, so GET can carry only text data

whereas

HTTPPost has no such restriction and it can carry both text as well as binary data.

Caching/Bookmarking – again for the obvious reason that a HTTPGet request is nothing but an URL hence it can be cached as well as Bookmarked.

No such luxuries with a HTTPPost request.

FORM Default

HTTPGet is the default method of the HTML FORM element.

To submit a FORM using POST method, we need to specify the method attribute and give it the value “HTTPPost”.

 

For more on ASP.NET MVC visit https://microsoftmentalist.wordpress.com/asp-net-2/

Thank you,

Advertisements
Categories: ASP.NET MVC
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: