将 JSON 数据从 ASP.NET MVC 控制器发布到另一个 URL
问题描述:
我尝试搜索有关如何从 ASP.NET MVC 控制器发布 JSON 数据的问题,但徒劳无功.
I tried searching about how to post JSON data from ASP.NET MVC Controller, all in vain.
我的控制器中有一个 post 方法,它从客户端接收 JSON 数据,并在将其转发到另一个位置之前进行格式验证.
I have a post method in my Controller, which receives JSON data from the client, and does format validation, before it forwards it another location.
我不知道如何将数据从这种方法发布到另一个位置.
I am not able to figure out how to post data from this method to the other location.
请帮忙.谢谢.
答
这里是异步代码,记得等待方法
Here is the async code for it, so remember to await the method
private async Task DoStuff(string s)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri('whatever');
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync("api/whatever/",s);
if (!response.IsSuccessStatusCode)
{
}
}