ASP.Net Core控制器操作会为简单字符串生成内容类型的文本/纯文本 [英] ASP.Net Core controller actions produces content type text/plain for simple strings

查看:74
本文介绍了ASP.Net Core控制器操作会为简单字符串生成内容类型的文本/纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制者和动作.

I have the following controller and action.

[Route("/api/simple")]
public class SimpleController : Controller
{
    [HttpGet]
    [Route("test")]
    public string Test()
    {
        return "test";
    }
}

当我调用它时,我希望动作返回"test" (有效的JSON),但是它返回 test (不带引号),这是有效行为或错误?我想念什么吗?

When I call it, I expect action to return "test" (which is valid JSON), but instead it returns test (without quotation marks) is this a valid behavior, or bug? Am I missing something?

GET http://localhost:5793/api/simple/test HTTP/1.1
User-Agent: Fiddler
Host: localhost:5793
Accept: application/json


HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Sun, 09 Aug 2015 14:37:45 GMT
Content-Length: 4

test

注意:对于ASP.NET Core 2.0+,当请求中存在接受标头时,此方法不适用-但是如果省略接受标头并进行内容协商,则仍然适用.

Note: For ASP.NET Core 2.0+ this doesn't apply when an Accept header is present in the request - but it still applies if the accept header is omitted and content negotiation occurs.

推荐答案

正如@mbudnik所指出的,罪魁祸首是 StringOutputFormatter ,以某种方式选择了格式化输出的格式,而不是 JsonOutputFormatter.但是,他的代码段不再起作用,因为此后对ASP.NET Core进行了一些更改.改用它:

As @mbudnik pointed out, the culprit here is StringOutputFormatter, which somehow gets selected to format the output instead of JsonOutputFormatter. His code snippet, however, no longer works because there’s been some changes to ASP.NET Core since then. Use this instead:

using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Formatters;

public class Startup {

    // ...

    public void ConfigureServices(IServiceCollection services) {
        // Add MVC, altering the default output formatters so that JsonOutputFormatter is preferred over StringOutputFormatter
        services.AddMvc(options => {
            var stringFormatter = options.OutputFormatters.OfType<StringOutputFormatter>().FirstOrDefault();
            if (stringFormatter != null) {
                options.OutputFormatters.Remove(stringFormatter);
                options.OutputFormatters.Add(stringFormatter);
            }
        });
    }

    // ...

}

或者,如果您认为根本不需要 StringOutputFormatter ,则可以将其完全删除:

Or, if you think you don't need StringOutputFormatter at all, you can remove it altogether:

services.AddMvc(options => {
    options.OutputFormatters.RemoveType<StringOutputFormatter>();
});

IMO这应该被认为是一个错误,因为您请求JSON响应( Accept:application/json ),并且返回不带引号的字符串肯定是不是 JSON.但是,官方立场是预期的结果.

IMO this should be considered a bug, since you asked for a JSON response (Accept: application/json) and returning the string without quotes is definitely not JSON. However, the official position is that this is expected.

这篇关于ASP.Net Core控制器操作会为简单字符串生成内容类型的文本/纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆