ASP.NET Core HTTPRequestMessage 返回奇怪的 JSON 消息 [英] ASP.NET Core HTTPRequestMessage returns strange JSON message

查看:59
本文介绍了ASP.NET Core HTTPRequestMessage 返回奇怪的 JSON 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 ASP.NET Core RC2,但遇到了一些奇怪的结果.所以我有一个具有以下功能的 MVC 控制器:

public HttpResponseMessage Tunnel() {var message = new HttpResponseMessage(HttpStatusCode.OK);message.Content = new StringContent("blablabla", Encoding.UTF8);message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");message.Headers.CacheControl = 新 System.Net.Http.Headers.CacheControlHeaderValue {NoCache = 真};回消息;}

如果我用邮递员调用它,并将 Accept 标头设置为纯文本,我会得到以下响应:

{版本": {主要":1,次要":1,构建":-1,修订版":-1,主要修订版":-1,次要修订版":-1},内容": {标题":[{"Key": "内容类型",价值": [文本/纯文本"]}]},状态代码":200,"ReasonPhrase": "好的",标题":[{"Key": "缓存控制",价值": [无缓存"]}],请求消息":空,IsSuccessStatusCode":真}

我真的不明白这是如何生成对上述控制器的响应.它基本上是整个消息本身的 JSON 序列化,绝不包含我打算发送的blablabla".

我得到想要的结果的唯一方法是让我的控制器函数返回 string 而不是 HttpResponse,但这样我就无法设置像 CacheControl

所以我的问题是:为什么我会收到这种奇怪的反应?这对我来说似乎是很奇怪的行为

解决方案

根据 本文,ASP.NET Core MVC默认不支持HttpResponseMessage-returning方法.>

如果你想继续使用它,你可以使用 WebApiCompatShim:

  1. 向您的项目添加对 Microsoft.AspNetCore.Mvc.WebApiCompatShim 的引用.
  2. ConfigureServices()中配置:services.AddMvc().AddWebApiConventions();
  3. Configure()中为它设置路由:

    app.UseMvc(routes =>{路线.MapWebApiRoute(名称:默认",模板:{controller=Home}/{action=Index}/{id?}");});

I am currently working with ASP.NET Core RC2 and I am running into some strange results. So I have an MVC controller with the following function:

public HttpResponseMessage Tunnel() {
    var message = new HttpResponseMessage(HttpStatusCode.OK);
    message.Content = new StringContent("blablabla", Encoding.UTF8);
    message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
    message.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue {
        NoCache = true
    };

    return message;
}

If I call this with postman with an Accept header set to text plain I get this response:

{
  "Version": {
    "Major": 1,
    "Minor": 1,
    "Build": -1,
    "Revision": -1,
    "MajorRevision": -1,
    "MinorRevision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Type",
        "Value": [
          "text/plain"
        ]
      }
    ]
  },
  "StatusCode": 200,
  "ReasonPhrase": "OK",
  "Headers": [
    {
      "Key": "Cache-Control",
      "Value": [
        "no-cache"
      ]
    }
  ],
  "RequestMessage": null,
  "IsSuccessStatusCode": true
}

I really do not understand how this is the generated reponse to the above controller. It is basically a JSON serialization of the entire message itself and does in no way contain the "blablabla" I intended to send.

The only way I have gotten the desired result is by making my controller function return string instead of HttpResponse, but that way I am unable to set headers like CacheControl

So my question is: why do I get this strange response? It seems like very weird behaviour to me

解决方案

According to this article, ASP.NET Core MVC does not support HttpResponseMessage-returning methods by default.

If you want to keep using it, you can, by using WebApiCompatShim:

  1. Add reference to Microsoft.AspNetCore.Mvc.WebApiCompatShim to your project.
  2. Configure it in ConfigureServices(): services.AddMvc().AddWebApiConventions();
  3. Set up route for it in Configure():

    app.UseMvc(routes =>
    {
        routes.MapWebApiRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    

这篇关于ASP.NET Core HTTPRequestMessage 返回奇怪的 JSON 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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