【问题标题】:JSON.stringify API returns 415 (.net & angular)JSON.stringify API 返回 415 (.net & angular)
【发布时间】:2020-11-12 21:16:22
【问题描述】:

我在从我的 Angular 前端到我的 .net API 的 POST 请求中遇到了一些麻烦。

这是前面的代码:

    [...]       
    this.uploadFile(JSON.stringify(jsonobject));
    }

    uploadFile(file)
    {
        this.files.push(file);
        this.uploadService.uploadjsonresponse(file, "JsonFilterResponse").subscribe();
    }

和发布请求:

    public uploadjsonresponse(file, apiname) {
         return this.httpClient.post(this.SERVER_URL + apiname, file);  
    }

最后是 .net 代码:

    [HttpPost]
    [Route("api/JsonFilterResponse")]
    [RequestSizeLimit(4_000_000_000)]
    [Microsoft.AspNetCore.Cors.EnableCors]
    public IActionResult JsonFilterResponse([FromBody] string jsonresult)
    {
        try
        {
            if (jsonresult != null)
            {
                Response.Body.Flush(); // http response
                return Ok(); // server response
            }
            else
                return NotFound();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            return StatusCode(500, "Internal server error");
        }
    }

Json 对象是正确的(在控制台上检查),API 地址也是正确的。 API 正在返回一个我似乎无法动摇的 415。任何建议将不胜感激!

【问题讨论】:

  • 什么是jsonobject?怎么样?
  • 当我记录前面发送的数据时,它看起来像这样:{"xxxxxx/xxxxxxxxx/xxxxxxxx":{"0":"*"}}。

标签: .net angular api http-status-code-415


【解决方案1】:

尝试不使用[FromBody]

public IActionResult JsonFilterResponse(string jsonresult)

【讨论】:

  • 感谢您的回复!当我这样做时,我的 API 被调用,但传递的字符串为空......然后我得到一个 404。
【解决方案2】:

对于那些可能想知道的人,我找到了答案!替换:

[FromBody] string jsonresult

[FromBody] JsonElement jsonresult

并使用

var json = System.Text.Json.JsonSerializer.Serialize(jsonresult);

解释结果!

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2016-09-02
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多