【问题标题】:C# - my ASP.NET MVC page always returns null to my JSON POST requestC# - 我的 ASP.NET MVC 页面总是向我的 JSON POST 请求返回 null
【发布时间】:2020-06-04 09:56:14
【问题描述】:

我有一个客户端,它会使用 JSON 向我的网页发送一些字符串数据,但是当我发布它时,由于某种原因它总是返回 null。

我的帖子有什么问题吗?

客户:

public async Task TesteAsync(string numeroserie)
{
    try
    {
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(numeroserie);
        var data = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

        var url = "https://localhost:44336/Home/Receive";
        var client = new HttpClient();

        var response = await client.PostAsync(url, data);

        string result = response.Content.ReadAsStringAsync().Result;
        MessageBox.Show(result);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
        return;
    }
}

服务器端:

[System.Web.Http.HttpPost]
public string Receive(string json)
{
    return json;
}

我的服务器正在使用 ASP.NET MVC。

Server side using breakpoint when the request is made

Client side using break when sending

更新: 我最后要做的是向我的网络服务器发送一个字符串,然后将其返回给客户端。 我没有使用 json 的经验,所以我不知道我所做的是否正确

【问题讨论】:

标签: c# json asp.net-mvc


【解决方案1】:

看。首先让我们检查一下http请求。

string url = "YourURL";
string body = JsonConvert.SerializeObject(BodyModel);


HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
{
   Content = new StringContent(body, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(request);
if ((int)response.StatusCode == 200)
{
   string responseString = await response.Content.ReadAsStringAsync();
   ResponseModel responseModel = JsonConvert.DeserializeObject<ResponseModel>(responseString);
}

现在让我们在您的控制器中进行一些更改。

[HttpPost]
public JsonResult Receive([FromBody] YourJsonModel parameters)
{
   //Do logic
   return Json(YourResponseModel);
}

当您想要传递字符串并将其作为字符串获取时,就是这种情况。 在您的控制器中,您可以使用 [FromQuery] 或只留下字符串都可以。

[HttpPost]
public string SomeAction(string data) // public string SomeAction([FromQuery] string data)
{
    return data;
}

这是请求

string url = "http://localhost:15832/YOURCONTROLLERNAME/SomeAction?data=YOURSTRINGVALUE";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
{
    Content = new StringContent("", Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(request);
if ((int)response.StatusCode == 200)
{
    string responseString = await response.Content.ReadAsStringAsync(); // here is your response as string
}

附:如果您不发送正文,则可以将操作类型更改为 HttpGet。因为它适合您的任务。

【讨论】:

  • 如果我没有模型怎么办?我只想将带有一些数据的字符串传递给网络服务器
  • 我会为您的问题编辑帖子。欢迎提出任何问题:)
  • 在你的字符串 url 中有 "localhost:15832/YOURCONTROLLERNAME/…";我的值在之前的方法中传递,例如: public async Task TesteAsync(string numeroserie) 我需要将该字符串发送到服务器
  • 我试过了,但我一直收到 null.. 它到达那里.. 可能是我调用它的方式吗?代码:HttpRequest httpRequest = new HttpRequest(); _ = httpRequest.TesteAsync(numeroserie);
  • 您是否尝试过与我提到的相同的方法?我已经测试过了,才放在这里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 2012-01-23
  • 2019-01-20
  • 2013-04-03
相关资源
最近更新 更多