我有一个更好的方法IMO。使用此WebApiDoodle.Net.Http.Client NuGet 包,您可以执行以下操作:
public class ShipmentsClient : HttpApiClient<ShipmentDto>, IShipmentsClient {
private const string BaseUriTemplateForSingle = "api/affiliates/{key}/shipments/{shipmentKey}";
private readonly string _affiliateKey;
public ShipmentsClient(HttpClient httpClient, string affiliateKey)
: base(httpClient, MediaTypeFormatterCollection.Instance) {
if (string.IsNullOrEmpty(affiliateKey)) {
throw new ArgumentException("The argument 'affiliateKey' is null or empty.", "affiliateKey");
}
_affiliateKey = affiliateKey;
}
public async Task<ShipmentDto> GetShipmentAsync(Guid shipmentKey, string foo) {
// this will build you the following URI:
// HttpClient.BaseAddress + api/affiliates/" + _affiliateKey + "/shipments/" + shipmentKey + "?=foo" + foo
var parameters = new { key = _affiliateKey, shipmentKey = shipmentKey, foo = foo };
var responseTask = base.GetSingleAsync(BaseUriTemplateForSingle, parameters);
var shipment = await HandleResponseAsync(responseTask);
return shipment;
}
// Lines removed for brevity
}
此处提供了一个示例用例:https ://github.com/tugberkugurlu/PingYourPackage
对于您的其他问题(我假设您正在公开 RPC 样式 API),您可以使用以下命令设置方法的操作名称System.Web.Http.ActionNameAttribute
:
[ActionName("GetDocByDate")]
public IEnumerable<Car> Get() {
IEnumerable<Car> cars = _carRepository.GetAll().ToList();
return cars;
}