简体   繁体   English

从Angular 2到ASP.net Core的POST请求不起作用。 服务器端的空值

[英]POST request from Angular 2 to ASP.net Core doesn't work. Null value on server side

Backend, ASP.net Core API: 后端,ASP.net核心API:

[Produces("application/json")]
    [Route("api/[controller]")]
    public class StoriesController : Controller
    {
        public static List<Story> STORIES = new List<Story>
            {
                new Story
                {
                    content = "Some really interesting story about dog",
                    timeOfAdding = new DateTime(2016, 8, 26),
                    numberOfViews = 11
                },
                new Story
                {
                    content = "Even cooler story about clown",
                    timeOfAdding = new DateTime(2016, 9, 26),
                    numberOfViews = 11
                },
                new Story
                {
                    content = "And some not cool story",
                    timeOfAdding = new DateTime(2016, 10, 26),
                    numberOfViews = 11
                }
            };

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
            Story story = new Story
            {
                content = value,
                timeOfAdding = DateTime.Now,
                numberOfViews = 0
            };
            STORIES.Add(story);
        }
    }

TypeScript function: TypeScript函数:

add(content: string): Observable<Story> {
        let body = JSON.stringify({ "value": content });
        //let body = { "value": content };
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers});

        return this.http.post(this.heroesUrl, body, options)
            .map(this.extractData)
            .catch(this.handleError);
    }

Parameters sent (seen in Firefox console): 发送的参数(在Firefox控制台中看到):

在此输入图像描述

value = null in Visual Studio 2015 debugger Visual Studio 2015调试器中的value = null

What's wrong? 怎么了? I've tried everything that I found in internet: adding/removing headers, removing JSON.stringify, adding/removing [FromBody] attribute. 我已经尝试了我在互联网上找到的所有东西:添加/删除标题,删除JSON.stringify,添加/删除[FromBody]属性。 Result is every time the same. 结果是每次都一样。

Since you're passing your value as JSON (as the screenshot suggests), you should use the model binding correctly and use a proper class instead of a string input: 由于您将值作为JSON传递(如屏幕截图所示),您应该正确使用模型绑定并使用适当的类而不是字符串输入:

public class StoryAddRequest
{
    public string Value { get; set; }
}

You then can use it in your controller: 然后,您可以在控制器中使用它:

// POST api/values
[HttpPost]
public void Post([FromBody] StoryAddRequest request)
{
    if (request != null)
    {
        Story story = new Story
        {
            content = request.Value,
            timeOfAdding = DateTime.Now,
            numberOfViews = 0
        };
        STORIES.Add(story);
    }
}

From the documentation : 文档

Request data can come in a variety of formats including JSON, XML and many others. 请求数据可以有多种格式,包括JSON,XML和许多其他格式。 When you use the [FromBody] attribute to indicate that you want to bind a parameter to data in the request body, MVC uses a configured set of formatters to handle the request data based on its content type. 当您使用[FromBody]属性指示要将参数绑定到请求正文中的数据时,MVC使用一组已配置的格式化程序来根据其内容类型处理请求数据。 By default MVC includes a JsonInputFormatter class for handling JSON data, but you can add additional formatters for handling XML and other custom formats. 默认情况下,MVC包含一个用于处理JSON数据的JsonInputFormatter类,但您可以添加其他格式化程序来处理XML和其他自定义格式。

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

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