简体   繁体   English

.net 核心在获取请求时返回空主体 json

[英].net core returns empty body json on Get request

Having troubles to return json .net core 6.0 C# I'm using NewtonSoft json library Obviously I'm doing something wrong...无法返回 json .net core 6.0 C# 我正在使用 NewtonSoft json 库显然我做错了什么......

using NetMQ;
using NetMQ.Sockets;
using Newtonsoft.Json.Linq;
using System.Text;

static JObject j_tmp = new JObject();

private static async Task<IResult> Command1()
{
   j_tmp["test"] = "1234";
   j_tmp["number"] = 18;
   //return Results.Json(j_tmp ); //Doesn;t work, empty body
   //return Results.Json(j_tmp.ToString()); //Doesn't work, adds "\r\n" on each break line
   return Results.Content(j_tmp.ToString(Newtonsoft.Json.Formatting.None), "application/json; charset=utf-8"); //WORKS
            
}

Assume you are working on ASP.NET Core project (as your question mentioned a GET request) and you are asking how to return a simple json.假设您正在处理 ASP.NET Core 项目(因为您的问题提到了GET请求)并且您正在询问如何返回一个简单的 json。

The simplest way is to return an anonymous object in your action method.最简单的方法是在您的操作方法中返回一个匿名对象。

In your controller:在您的控制器中:

[HttpGet]
public IActionResult YourAction() 
{
    return OK(new { test = "1234", number = 18 });
}

Is that what you need?那是你需要的吗?

when I return JObject object, it appears empty on receiving (frontend) side当我返回 JObject 对象时,它在接收(前端)端显示为空

Maybe you want a way to pass the JObject object from contoller to the view ?也许您想要一种将 JObject 对象从控制器传递到视图的方法?

If so, you can refer to below demo.如果是这样,您可以参考下面的演示。

In controller:在控制器中:

static JObject j_tmp = new JObject();
        public IActionResult Index()
        {
            j_tmp["test"] = "1234";
            j_tmp["number"] = 18;
            ViewBag.j_tmp= j_tmp;   
            return View(j_tmp);
        }

In the view:在视图中:

@ViewBag.j_tmp

Result:结果:

在此处输入图像描述 在此处输入图像描述

use this method使用这个方法

private static async Task<JsonResult> Command1()
{
   j_tmp["test"] = "1234";
   j_tmp["number"] = 18;
   //return Results.Json(j_tmp ); //Doesn;t work, empty body
   //return Results.Json(j_tmp.ToString()); //Doesn't work, adds "\r\n" on each break line
   return Json(new JsonResult(null, new { test = j_tmp["test"], number = j_tmp["number"] }));
            
}

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

相关问题 C#Net Core API Post JSON请求正文为空 - C# net core api post json request body is empty 来自.Net Core 3.0的请求正文为空 - Request body from is empty in .Net Core 3.0 ASP.NET核心在json post请求体中传递antiforgerytoken - ASP.NET Core pass antiforgerytoken in json post request body Fresh ASP.NET Core API 返回空 JSON 对象 - Fresh ASP.NET Core API returns empty JSON objects ASP.NET Core从ActionExecutedContext ActionFilter获取请求正文 - ASP.NET Core Get Request Body from ActionExecutedContext ActionFilter 有没有办法在 .NET Core FilterAttribute 中获取请求正文? - Is there any way to get request body in .NET Core FilterAttribute? .net 核心对 http GET 和发送主体的错误请求 - .net core bad request for http GET and sending body 中间件异常后获取请求正文。 .NET 核心 3.1 - Get request body after exception in middleware. .NET Core 3.1 Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? - Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? ASP.NET 核心 5,带有来自正文的参数的 HttpPut 请求始终返回 Http 400 错误请求 - ASP.NET Core 5, HttpPut request with parameters from body always returns Http 400 Bad request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM