[英]C# - my ASP.NET MVC page always returns null to my JSON POST request
I have a client that will send some string data to my web page using JSON, but when I post it, it always returns null for some reason.我有一个客户端会使用 JSON 将一些字符串数据发送到我的 web 页面,但是当我发布它时,它总是出于某种原因返回 null。
Is there anything wrong with my post?我的帖子有什么问题吗?
Client:客户:
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;
}
}
Server Side:服务器端:
[System.Web.Http.HttpPost]
public string Receive(string json)
{
return json;
}
My server is using ASP.NET MVC.我的服务器正在使用 ASP.NET MVC。
Server side using breakpoint when the request is made发出请求时服务器端使用断点
Client side using break when sending客户端在发送时使用中断
Update: What i want to do in the end is send to my webserver a string and then return it to the client.更新:我最终要做的是向我的网络服务器发送一个字符串,然后将其返回给客户端。 I have no experience with json so i don't know if it's correct what i did
我对 json 没有经验,所以我不知道我所做的是否正确
Look.看。 At first lets check http request.
首先让我们检查 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);
}
now lets make some changes in your controller.现在让我们对您的 controller 进行一些更改。
[HttpPost]
public JsonResult Receive([FromBody] YourJsonModel parameters)
{
//Do logic
return Json(YourResponseModel);
}
Here is the case when you want to pass string and get it as string.这是您想要传递字符串并将其作为字符串获取的情况。 In your controller you can use [FromQuery] or just left string both are ok.
在您的 controller 中,您可以使用 [FromQuery] 或只留下字符串都可以。
[HttpPost]
public string SomeAction(string data) // public string SomeAction([FromQuery] string data)
{
return data;
}
And here is the request这是请求
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
}
PS If you are not sending body, you can change action type to HttpGet. PS 如果您不发送正文,您可以将操作类型更改为 HttpGet。 As it comfortable to your task.
因为它对你的任务很舒服。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.