Post complex types to WebApi Client

Posted by BumbleBee on Stack Overflow See other posts from Stack Overflow or by BumbleBee
Published on 2013-10-30T21:50:45Z Indexed on 2013/10/30 21:53 UTC
Read the original article Hit count: 219

I am new to WebAPI.

I have a MVC project and webApi project both reside under the **same solution**. Also, BLL Class library and DAL class library reside under the same solution.

Earlier, my MVC project will talk to the BLL now I am trying to create a WebAPi project which stands in between MVC and BLL.

Here is what I have come up with so far :

I'm using the HTTPClient to post to a WebApi project. My post method on my controller accepts a single parameter (a model).

  StatsCriteria criteria = new StatsCriteria();
......
    var client = new HttpClient();
    var response = client.PostAsJsonAsync("http://localhost:52765/api/reports", criteria).Result;
    .......

Here's the signature for my controller in Webapi

[HttpPost]
public CMAReportVM Reports([FromBody] StatsCriteria criteria)
{
  var cmaReport = Service3.GetCMAReport(criteria.Mlsnums);
   //Create Map to enable mapping business object to View Model
             Mapper.CreateMap<CMAReport, CMAReportVM>();
   // Maps model to VM model class 
             var cmaVM = Mapper.Map<CMAReport, CMAReportVM>(cmaReport);
  reutn cmaVM; 
}

// and here's my routing:

config.Routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/{controller}/{id}",
  defaults: new { id = RouteParameter.Optional }

I am getting the following
405 : Method not allowed.

As the WebAPI and MVC project both reside under same sloution I am not sure where/how to host my webapi.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-web-api