首页 新闻 会员 周边 捐助

webapi中 【formbody】参数 怎么获取。

0
悬赏园豆:10 [已解决问题] 解决于 2015-01-07 09:34

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里得到 传入的数据

simadi的主页 simadi | 初学一级 | 园豆:134
提问于:2014-07-09 21:48
< >
分享
最佳答案
0

你这是从winform窗体传值到MVC的controller里面?

收获园豆:10
晓菜鸟 | 老鸟四级 |园豆:2594 | 2014-07-10 11:36

从winform窗体传值到webapi的controller里

simadi | 园豆:134 (初学一级) | 2014-07-10 13:45

@simadi: 试试从request的body中取值.

晓菜鸟 | 园豆:2594 (老鸟四级) | 2014-07-10 13:58

@simadi: 试试这个,我以前好像用过,你看看行吗?

HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];

晓菜鸟 | 园豆:2594 (老鸟四级) | 2014-07-10 14:02

@晓菜鸟:  我要在controller 里面用的

public string Post([FromBody]string m)
{
return m;
}

simadi | 园豆:134 (初学一级) | 2014-07-10 14:14

@simadi: 你这个 m 是怎么传递的?是表单直接提交过了的吗?

晓菜鸟 | 园豆:2594 (老鸟四级) | 2014-07-10 19:48

@晓菜鸟: 

输入参数 为 {"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;

simadi | 园豆:134 (初学一级) | 2014-07-10 22:31

@晓菜鸟: 

输入参数 为 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;

simadi | 园豆:134 (初学一级) | 2014-07-10 22:32

@晓菜鸟:  这两种方法 返回的 outString 都为null

simadi | 园豆:134 (初学一级) | 2014-07-10 22:32

@simadi: 弄好了吗?

晓菜鸟 | 园豆:2594 (老鸟四级) | 2014-07-11 13:11

@simadi: 你可以看一下我写的这篇文章  http://www.cnblogs.com/babycool/p/3922738.html  希望能帮到你

酷小孩 | 园豆:243 (菜鸟二级) | 2014-08-20 20:07
其他回答(1)
0

搞定了吗?我也遇到这个,那参数传过去都是空的,但是content-length这个是正确的,是不是传递的方式不对?

sunzone | 园豆:202 (菜鸟二级) | 2014-09-26 15:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册