5

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

JSideris
  • 5,101
  • 3
  • 32
  • 50
  • 6
    @SajadLfc Yea, but couldn't find exactly what I was looking for. This question has not been asked before on this site, and possible duplicate you posted isn't what I'm asking. Thanks. – JSideris Jan 28 '14 at 06:49

3 Answers3

6

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

Jason Roell
  • 6,679
  • 4
  • 21
  • 28
1

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

I think there are plenty of discussions already going on these topics They are entirely different please check these links for a basic idea of both

HttpPost vs HttpGet attributes in MVC: Why use HttpPost?

what is web method attribute in web service?

Community
  • 1
  • 1
Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32