-3
必须使用Web API。控制器看起来像这样:使用ASP.NET WebAPI
using System;
using System.Collections.Generic;
using System.Web.Http;
using ERPService.Api.Models;
using ERPService.Core.Services;
namespace ERPService.Api.Controllers
{
[RoutePrefix("api/local")]
public class LocalProductController : BaseApiController
{
[Route("product/{productId}/export")]
public ApiResponse<IList<ServiceResponse<object>>> ExportProduct(int productId)
{
return Response(this.ServiceFactory.ProductService.ExportProduct(productId));
}
}
}
使用HttpClient如何调用ExportProduct函数? 我创建了一个控制台应用程序来消费这样的:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:49319/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/local/product/{productId}/export").Result;
结果是错误如下:
错误代码 - MethodNotAllowed
消息 - 不允许的方法
[so] is * not * a free code writing service。预计你会尝试**自己编写代码**。在[做更多研究]之后(http://meta.stackoverflow.com/questions/261592),如果你有问题,你可以**发布你已经尝试过**的清单,说明什么是不工作的**并提供** [mcve] **。我建议阅读[问]一个好问题和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要参加[旅游]。 – Igor
您使用HttpClient加载与该方法关联的路由。你有什么尝试? – squillman
尝试像这样: HttpClient client = new HttpClient(); client.BaseAddress = new Uri(“http:// localhost:49319 /”); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); HttpResponseMessage response = client.GetAsync(“api/local/product/{productId}/export”)。 很明显,我无法使用Get,但应该使用什么? – BenjiK