1

I am trying to post an object to the server where I am using Web API 2. The code as follows:

$.ajax({
     cache: false,
     type: "POST",
     url: "/api/Employees",
     data: { EmployeeId: 1 },
     success: function(result) {
          console.log("employees saved successfully");
     },
     error: function(result) { }
});

As for the Web API:

public class EmployeesController : ApiController
{
    // POST api/<controller>
    public void Post([FromBody]Employee value)
    {
    }
}

public class Employee
{
    public Int32 EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string JobTitle { get; set; }
    public DateTime BirthDate { get; set; }
    public DateTime HireDate { get; set; }
    public ReferenceData MaritalStatus { get; set; }
    public Int32 VacationDays { get; set; }
    public Int32 SickLeaveDays { get; set; }
    public decimal Salary { get; set; }
    public string Cid { get; set; }
}

I am ending up with this response from server

The requested resource does not support http method 'POST'
Bill
  • 2,026
  • 9
  • 55
  • 99
  • Try this question [http://stackoverflow.com/questions/11005788/asp-net-web-api-the-requested-resource-does-not-support-http-method-get][1] [1]: http://stackoverflow.com/questions/11005788/asp-net-web-api-the-requested-resource-does-not-support-http-method-get – tichra Aug 10 '15 at 13:30
  • If I rename the method to be "SaveEmployee" for example, add the [HttpPost] and post to /api/Employees/SaveEmployee it works fine! Only with method named Post it is not working. – Bill Aug 10 '15 at 13:31

3 Answers3

0

You need to put the annotation [httppost] on the controller

public class EmployeesController : ApiController
{
    // POST api/<controller>
    [HttpPost]
    public void Post([FromBody]Employee value)
    {
    }
}

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Half way down the page, under HTTP Methods

Stephen King
  • 816
  • 4
  • 14
0

Is it possible that there is an atypical or custom configuration in your WebApiConfig.Register method related to routing? It is unnecessary to add the [HttpPost] meta data attribute when the ApiController is using the RESTful convention for action names. Likewise the [FromBody] attribute isn't necessary when the parameter is a complex type. By default WebApi will look at the body of the request and use the correct MediaTypeFormatter (XML,JSON,FormUrlEncoded). Below is the default WebApiConfig.Register. If you have made changes, can you revert to the default WebApiConfig.Register, remove the meta data attributes, then retry?

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
Bill
  • 1,241
  • 17
  • 25
0

That's because of how API routing works. It's /api/NameOfController/NameOfMethod. Your controller's name is Employee, and your method's name is Post. If you want to get to that method, you need to do /api/Employee/Post. That's why /api/Employees/SaveEmployee works fine when you change the method name (per your comment from yesterday).

codeMonkey
  • 4,134
  • 2
  • 31
  • 50