Service
이라는 ApiController가 있습니다. 나는 그것에 UpdatePrice
라는 메서드가 있습니다. 이제 404 오류 코드를 수신하여 다른 프로젝트의 http 웹 요청을 보내려고합니다.http 요청으로 ApiController에서 메소드를 호출하는 방법은 무엇입니까?
public class Service : ApiController
{
PriceApplication priceApp = new PriceApplication();
public int UpdatePrice(int priceId,int cost,DateTime lastUpdate)
{
try
{
var price = priceApp.GetByPriceId(priceId);
price.Cost = Convert.ToDecimal(cost);
price.LastUpdate = lastUpdate;
priceApp.Update(price);
return cost;
}
catch
{
return -1;
}
}
}
이 그것을 위해 라우팅입니다 :
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { controller = "service",action="updateprice", id = RouteParameter.Optional}
);
는 그리고이 요청입니다
<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost:26769/api/service/updateprice?priceId=16&cost=1234&lastUpdate=2014-10-15 11:41:54.000'.</Message>
<MessageDetail>No type was found that matches the controller named 'service'.</MessageDetail>
</Error>
이것은 ApiController입니다 : 내가 브라우저에서 실행하는 경우 그리고 나는 이것이 참조 :
string serviceUrl = string.Format("http://localhost:26769/api/service/updateprice?priceId={0}&cost={1}&lastUpdate={2}", priceId, cost, DateTime.Now);
WebRequest request = WebRequest.Create(serviceUrl);
WebResponse response = request.GetResponse();
컨트롤러 'ServiceController'의 이름을 변경하십시오. 속성 라우팅을 고려할 수도 있습니다. http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 –
또한 GET이 아닌 POST를 사용하여 업데이트해야합니다. –
@TrevorPilley 반환 매개 변수 (lastUpdate 및 priceId 및 비용)는 서비스 크롤러 프로젝트에서 가져오고 주 프로젝트 도메인 모델에 대한 참조가 없으므로 GET으로 전달해야합니다. –