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

asp.net, asp.net-mvc, c#

Solution

`[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.

Problem

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

Original source

Related problems