简体   繁体   English

C# - 我的 ASP.NET MVC 页面总是返回 null 给我的 Z0ECD11C1D7A2874201D148A23BBD7A

[英]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.

相关问题 Cookie 总是 Null C# ASP.NET MVC - Cookie is always Null C# ASP.NET MVC 我的模型总是空的,为什么? ASP.Net MVC - My model is always null, why? ASP.Net MVC 贝宝PDT总是对我的HTTP发布请求C#MVC返回Fail - Paypal PDT returns Fail always to my HTTP post request C# MVC 混合了我的两个模型,Null 在 Post Request (ASP.NET MVC) 中的参考 - Mixed up my Two Models, Null Reference in Post Request (ASP.NET MVC) 如何在我的ASP.Net MVC 5应用程序的同一页中进行编辑和删除POST请求 - How to make edit and delete POST Request in same page in my ASP.Net MVC 5 application 如何在我的Facebook页面上发布状态? [ASP.NET MVC 4] - How to post a status on my facebook page? [ASP.NET MVC 4] 我的asp.net(C#4.0)页面上始终以蓝色显示节点的Treeview字体颜色吗? - Treeview font color of the node is shown in blue color always in my asp.net(C# 4.0) page? ASP.NET MVC controller 在发布/获取请求时始终返回“False” - ASP.NET MVC controller always returns "False" on Post/Get request 如何在我的页面粉丝墙上使用c#和asp.net在Facebook上发帖 - How to do a post in facebook on my page fan wall with c# and asp.net Asp.net MVC 2 Request.files [“”]始终返回null - Asp.net MVC 2 Request.files[“”] return null always
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM