controller中的代码
[HttpPost]
public string Post([FromBody]string m)
{
return m;
}
我写了一个winform程序,winform 的代码为
输入参数 为 {"m":"123"}
HttpClient client = new HttpClient {BaseAddress = new Uri(baseUrl)};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var inString=输入参数
var m = JsonConvert.DeserializeObject(inString) as JObject;
response = client.PostAsJsonAsync(localPath, m).Result;
var outString = response.Content.ReadAsStringAsync().Result;
返回的 outString 一直为null
换为如下方式 输入参数 为 123 结果仍然为null
HttpClient client = new HttpClient {BaseAddress = new Uri(baseUrl)};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var inString=输入参数
var c = new StringContent(inString);
c.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
response = client.PostAsync(localPath, c).Result;
var outString = response.Content.ReadAsStringAsync().Result;
求大神帮忙,如何在controller里得到 传入的数据
你这是从winform窗体传值到MVC的controller里面?
从winform窗体传值到webapi的controller里
@simadi: 试试从request的body中取值.
@simadi: 试试这个,我以前好像用过,你看看行吗?
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
@晓菜鸟: 我要在controller 里面用的
public string Post([FromBody]string m)
{
return m;
}
@simadi: 你这个 m 是怎么传递的?是表单直接提交过了的吗?
@晓菜鸟:
输入参数 为 {"m":"123"}
HttpClient client = new HttpClient {BaseAddress = new Uri(baseUrl)};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var inString=输入参数
var m = JsonConvert.DeserializeObject(inString) as JObject;
response = client.PostAsJsonAsync(localPath, m).Result;
var outString = response.Content.ReadAsStringAsync().Result;
@晓菜鸟:
输入参数 为 123
HttpClient client = new HttpClient {BaseAddress = new Uri(baseUrl)};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var inString=输入参数
var c = new StringContent(inString);
c.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
response = client.PostAsync(localPath, c).Result;
var outString = response.Content.ReadAsStringAsync().Result;
@晓菜鸟: 这两种方法 返回的 outString 都为null
@simadi: 弄好了吗?
@simadi: 你可以看一下我写的这篇文章 http://www.cnblogs.com/babycool/p/3922738.html 希望能帮到你
搞定了吗?我也遇到这个,那参数传过去都是空的,但是content-length这个是正确的,是不是传递的方式不对?