[英]AngularJS $http and WebApi 2
I have an issues which has just arisen. 我有一个刚刚出现的问题。 I have a Web API that has this method:
我有一个具有此方法的Web API:
[HttpGet]
[Route("")]
public IHttpActionResult Get(OrderRequestViewModel model)
{
var order = new orderHeader()
{
sordNo = model.OrderNumber,
process = orderHeader.orderProcesses.Read
};
order.processInfos.read_CoreHistory = model.ReadCoreHistory;
var response = this.Connection.webServicesAdvanced.ordersHubAction_S(this.SecurityToken, order);
switch (response.state)
{
case processorResult.resultEnum.failed:
case processorResult.resultEnum.validationfailed:
throw new HttpException(500, response.info);
default:
return Ok(new OrderResponseViewModel(response.obj));
}
}
The OrderRequestViewModel only has 2 properties: OrderNumber (string) and ReadCoreHistory (boolean). OrderRequestViewModel仅具有2个属性:OrderNumber(字符串)和ReadCoreHistory(布尔)。
In my Angular JS application I have a service which looks like this: 在我的Angular JS应用程序中,我有一个类似于以下的服务:
.factory('OrderService', ['$http', '$filter', 'apiUrl', function ($http, $filter, api) {
var _get = function (orderNumber, readCoreHistory) {
return $http.get(api + 'orders?orderNumber=' + orderNumber + '&readCoreHistory=' + readCoreHistory);
}
//-- removed for brevity --//
var service = {};
service.get = _get;
//-- removed for brevity --//
return service;
}])
When I call the get() method from my controller, if I have fiddler open it states: 当我从控制器调用get()方法时,如果我打开了提琴手,它会指出:
No MediaTypeFormatter is available to read an object of type 'OrderRequestViewModel' from content with media type 'application/octet-stream'.
没有MediaTypeFormatter可用于从媒体类型为“ application / octet-stream”的内容读取“ OrderRequestViewModel”类型的对象。
Apparently this is a content type header issue, but I have no access to the ajax call options that I can see. 显然,这是一个内容类型标头问题,但我无权访问可见的ajax调用选项。 Does anyone know how I can solve this?
有谁知道我该怎么解决?
GET
method does not have a body, complex types are filled by data from request body in ASP.NET WebAPI, you should add [FromUri]
attribute before your complex type in method signature. GET
方法没有正文,复杂类型由ASP.NET WebAPI中来自请求正文的数据填充,您应该在方法签名中的复杂类型之前添加[FromUri]
属性。
public IHttpActionResult Get([FromUri] OrderRequestViewModel model)
{ ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.