简体   繁体   English

IActionResult 未将字符串响应作为字符串返回

[英]IActionResult not returning string response as string

I am migrating my web api's to .net core code.我正在将我的 web api 迁移到 .net 核心代码。

This is my current api, which is correctly returning string as expected这是我当前的 api,它按预期正确返回字符串

[HttpPost]
public string ValidateEwpInspectionNumber(string ewpNumber, string inspectionNumber)
{
    return _ewpDataAccess.ValidateEwpInspectionNumber(ewpNumber, inspectionNumber);
}

This is how the response looks like in developer tool for it这就是响应在开发者工具中的样子

在此处输入图像描述

Which is what is expected by my code.这是我的代码所期望的。 The api is properly returning string result. api 正确返回字符串结果。

Now, this is the migrated api where we are using IActionResult as return type现在,这是我们使用IActionResult作为返回类型的迁移 api

[HttpPost(nameof(ValidateEwpInspectionNumber))]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
public async Task<IActionResult> ValidateEwpInspectionNumber(string ewpNumber, string inspectionNumber)
{
    var response = await _ewpBusinessService.ValidateEwpInspectionNumberAsync(ewpNumber, inspectionNumber);
    return Ok(response);
}

But this api is not returning result as string.但是这个 api 没有将结果作为字符串返回。 This is how the response looks.这就是响应的样子。

在此处输入图像描述

You can see it is not same as the old api and because of which the angular code is not considering the response as string but an object.您可以看到它与旧的 api 不同,因此角度代码不将响应视为字符串而是一个对象。

I tried returning Content(response) and Content(response, 'text/plain') also but that too returned the same type of object.我也尝试返回Content(response)Content(response, 'text/plain')但那也返回了相同类型的对象。

Tried replacing IActionResult with ActionResult<string> , but no luck.尝试用ActionResult<string>替换IActionResult ,但没有成功。

Is there any way to return direct string from IActionResult the way old api is returning?有什么方法可以像旧 api 返回的那样从IActionResult返回直接字符串吗?

Update更新

I checked that by default the Ok() returns response in json format only which is what I want, but for my response, since it is a plain text, it is setting the content type to text/plain .我检查了默认情况下Ok()仅返回我想要的 json 格式的响应,但是对于我的响应,因为它是纯文本,所以它将内容类型设置为text/plain

So I tried to set the content type manually by using Content(response, 'application/json') but then it starts giving json parsing error所以我尝试使用Content(response, 'application/json')手动设置内容类型,但随后它开始出现 json 解析错误

can't parse JSON.无法解析 JSON。 Raw result:原始结果:

NOTEXISTS不存在

So looks like it is not possible with .net core api to return string as json until it is actually a valid json.所以看起来 .net 核心 api 不可能将字符串作为 json 返回,直到它实际上是一个有效的 json。

Could you check to know if the API response is JSON or just Text?你能检查一下 API 响应是 JSON 还是只是文本吗? The screenshot you shared seems like the response is formatted as plain text and the old api has its response format as JSON.您共享的屏幕截图似乎响应格式为纯文本,而旧 api 的响应格式为 JSON。 IF this is the case you may want to force the API to return JSON.如果是这种情况,您可能希望强制 API 返回 JSON。

Return it as a JsonResult , you need to format it:将其作为JsonResult返回,您需要对其进行格式化:

public async Task<IActionResult> ValidateEwpInspectionNumber(string ewpNumber, string inspectionNumber)
{
    var response = await _ewpBusinessService.ValidateEwpInspectionNumberAsync(ewpNumber, inspectionNumber);
    return new JsonResult(response);
}

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

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