Asp.net mvc 如何使用数据调用web api

Asp.net mvc 如何使用数据调用web api,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,asp.net-web-api,fiddler,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Asp.net Web Api,Fiddler,在我的WebAPI控制器中,我有两个具有以下结构的函数 public HttpResponseMessage Post(CountryDto country) { var countries = _countryAppService.RegisterNewCountry(country); var message = Request.CreateResponse(HttpStatusCode.Created, countries); return message; }

在我的WebAPI控制器中,我有两个具有以下结构的函数

public HttpResponseMessage Post(CountryDto country)
 {
    var countries = _countryAppService.RegisterNewCountry(country);
    var message = Request.CreateResponse(HttpStatusCode.Created, countries);
    return message;
 }

public HttpResponseMessage Post(int countryId, StateDto state)
{
    var country = _countryAppService.AddNewState(state, countryId);
    var message = Request.CreateResponse(HttpStatusCode.Created, country);
    return message;
}
我需要调用post的第二个重载版本,我使用fiddler尝试了以下http请求细节

POST http://localhost:51830/api/Country/ HTTP/1.1
User-Agent: Fiddler
Host: localhost:51830
Content-Type: application/json; charset=utf-8
Content-Length: 63

{"countryId":5,"state":{"StateName":"Dallas","StateCode":"DA"}}
但是它调用的是第一个重载的post而不是第二个post,我缺少的是什么,以及如何使用fiddler调用第二个post。您需要在第二个方法中的StateDto参数上指定[FromBody]属性


WebAPI和MVC都不支持重载

但是,您可以使用路由实现您想要的,即:

[Route("api/country/add")]
public HttpResponseMessage AddCountry(CountryDto country)

[Route("api/country/addstate")]
public HttpResponseMessage AddCountry(int countryId, StateDto state)

您需要确保调用config.maphttpAttribute路由;在configurationi.e.:config.Routes.MapHttpRoute…

中的其余路由之前,请认为此问题与模型绑定器有关,但不确定。解决方法是使用不同的路由,例如使用或使用属性。