Quantcast
Channel: ASP.NET 4.5 Hosting News (SuperBlogAds Network) » ASP.NET 4.0 Hosting
Viewing all articles
Browse latest Browse all 25

ASP.NET Hosting :: Difference between HttpGet and HttpPost Method

$
0
0

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 theHandleUnknownAction method on the Controller, supplying the name of the missing action. If more than one action method meeting these criteria is found,an InvalidOperationExceptionis 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

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

*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.

*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

*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.

*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”.

Advantages:

Key points about data submitted using using HttpGet

*Data is submitted as a part of url.

*Data is visible to the user as it post as query string.

*it is not secure but fast and quick.

*It use Stack method for passing form variable.

*Data is limited to max length of query string.

*It is good when you want user to bookmark page.

*Requests data from a specified resource

*An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet.

Key points about data submitted using HttpPost

*Data is submitted in http request body.

*Data is not visible in the url.

*It is more secured but slower as compared to GET.

*It use heap method for passing form variable

*It can post unlimited form variables.

*It is advisable for sending critical data which should not visible to users.

*Submits data to be processed to a specified resource

*A Submit button will always initiate an HttpPost request.


Viewing all articles
Browse latest Browse all 25

Trending Articles