在ASP.net核心中将对象转换为Json

在ASP.net核心中将对象转换为Json,json,asp.net-core,httpresponsemessage,Json,Asp.net Core,Httpresponsemessage,我正在尝试在.NETFramework4中将“Persona”转换并对象化为json字符串,我需要帮助 我已经尝试过(使用System.Text.Json) 这个(使用Newtonsoft.Json) 调试时,“persona”有数据,“Serialize”显示null 我也尝试了“Request.CreateResponse”,但由于一些奇怪的原因,它没有被识别为有效代码 我跳过哪一步?如果要使用HttpResponseMessage在asp.core mvc中返回信息,需要完成以下步骤 为

我正在尝试在.NETFramework4中将“Persona”转换并对象化为json字符串,我需要帮助

我已经尝试过(使用System.Text.Json)

这个(使用Newtonsoft.Json)

调试时,“persona”有数据,“Serialize”显示null

我也尝试了“Request.CreateResponse”,但由于一些奇怪的原因,它没有被识别为有效代码


我跳过哪一步?

如果要使用
HttpResponseMessage
在asp.core mvc中返回信息,需要完成以下步骤

  • 为您的应用程序安装Microsoft.AspNetCore.Mvc.WebApiCompatShim软件包 项目
  • 添加
    services.AddMvc().AddWebApiConventions()
    以在starup.cs文件中配置服务
代码如下:

    public HttpResponseMessage Get(int id)
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        Personas persona = _context.tPersonas.FirstOrDefault(p => p.idPersona == id);
        if (persona != null)
        {
            response.Content = new StringContent(JsonConvert.SerializeObject(persona));
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return response;
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("error message");
            return response;
        }

    } 
您也可以参考。

以下是postman的调试过程:


有了Newtonsoft,它就可以工作了。您的问题不在于var persona=new{Name=“Name”,LastName=“LastName”};var st=JsonConvert.SerializeObject(persona);st变量结束为空,不知道为什么。想法?清理您的解决方案,构建并重新运行它,我亲自测试了它,它工作了谢谢先生!就像你说的那样。如果请求成功,您知道如何发送JSON吗?如果请求失败,您知道如何发送JSON吗?非常感谢,先生!这就是我想要的:)
 public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            Personas persona = context.tPersonas.FirstOrDefault(p => p.idPersona == id);
            if (persona != null)
            {
                response.Content = new StringContent(JsonConvert.SerializeObject(persona));
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                return response;
            }
            else
                return response = new HttpResponseMessage(HttpStatusCode.BadRequest);            
        }
    public HttpResponseMessage Get(int id)
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        Personas persona = _context.tPersonas.FirstOrDefault(p => p.idPersona == id);
        if (persona != null)
        {
            response.Content = new StringContent(JsonConvert.SerializeObject(persona));
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return response;
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("error message");
            return response;
        }

    }