简体   繁体   English

将Web API的POST参数设置为null

[英]POST parameter to web api being set as null

I'm trying to use ng-resource to POST a value to my web-api and get the value back in javascript. 我正在尝试使用ng-resource将值发布到我的web-api,并在javascript中获取该值。

While the request seems to go through alright, the parameter being passed to my function public string AddOrder([FromBody]string order) seems to be set as null . 虽然请求似乎可以通过,但传递给我的函数public string AddOrder([FromBody]string order)似乎设置为null

orderController.js orderController.js

var result = $scope.store.sendOrder("hello");

store.js store.js

function store($resource) {

    var Resource = $resource('/api/products/');
    this.products = Resource.query();

    this.sendOrder = function (order) {

        var sendOrder = $resource('/api/products');

        var result = sendOrder.save("Helloa"); <---

        return result;
    }
}

ProductController.cs ProductController.cs

[Route("api/products")]
[HttpPost]
public string AddOrder([FromBody]string order)
{
    return order; //When I put a breakpoint here and check the value of order, it is null.
}

raw request 原始请求

POST http://localhost:12345/api/products HTTP/1.1
Host: localhost:12345
Connection: keep-alive
Content-Length: 6
Accept: application/json, text/plain, */*
Origin: http://localhost:12345
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:12345/Home/Cart
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.8,es;q=0.6,en-US;q=0.4
Cookie: ai_user=fc592beca77b4a5e8c4a95db221a574a|2014-12-04T18:36:22.7128874+00:00

hello

raw reply 原始回复

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbXJ5YW4uQVNBVklFXERvY3VtZW50c1xleHByZXNzb1xFeHByZXNzb1x3ZWJzaXRlc1xjb25zdW1lclxhcGlccHJvZHVjdHM=?=
X-Powered-By: ASP.NET
Date: Mon, 12 Jan 2015 10:45:03 GMT
Content-Length: 4

null

Anybody have any ideas? 有人有什么想法吗?

Edit 编辑

The quickest / easiest solution to this was to wrap the string in an object as follows: 最快/最简单的解决方案是将字符串包装在一个对象中,如下所示:

Order.cs Order.cs

public class Order
{
    public string order { get; set; }
}

ProductController.cs ProductController.cs

[Route("api/products")]
[HttpPost]
public Order AddOrder([FromBody]Order order)
{
    return order;
}

尽管我不确定是什么导致该字符串在控制器中为空,但您可以发送一个包装字符串的对象,而不是将其发布到Web API控制器,而该对象将被正确接受。

To post to a string to the web api it needs to be preceded by = .Also there is no Content-Type set on the request. 要将字符串发布到Web api,需要在其前面加上= 。此外,请求中未设置Content-Type

  var sendOrder = $resource('/api/products',{}, { query: {method:'POST', headers: {'Content-Type': 'application/json'});

  var result = sendOrder.save("=" + "Helloa");

The above should work 以上应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM