Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between [HttpPost] and [WebMethod]?

What are the main differences in functionality between these two method attributes?

like image 353
JSideris Avatar asked Jan 28 '14 06:01

JSideris


2 Answers

[HttpPost] is an Attribute that decorates a controller or controller action in ASP.Net MVC. You will use it to only allow a request to enter this action method if it is of type "POST".

It looks like this typically:

[HttpPost]
public ActionResult MyControllerAction()
{
  // only can get here if httprequest was a "POST"
}

A [WebMethod] attribute is used to decorate methods on an old school .asmx page typically used for making a web service. Attaching the [WebMethod] attribute to a Public method indicates that you want the method exposed as part of the XML Web service.

Typically looks like this on an .asmx page:

public class Service1 : System.Web.Services.WebService
{ 
    [WebMethod] // exposes XML Web Service Method
    public DataSet IAmAWebServiceMethod()
    {
       //implementation code
    }
}

They are not comparable and do completely different operations. One handles "POST" requests for a web application while another exposes a XML Web Service method.

like image 193
Jason Roell Avatar answered Oct 17 '22 16:10

Jason Roell


There is no comparison between them. [WebMethod] is part of the legacy ASMX web service technology and should not be used for new development.

like image 22
John Saunders Avatar answered Oct 17 '22 17:10

John Saunders