web api 的post问题

呆瓜丶 2014-10-21 01:34:51
想post我写的api,为啥post返回的就是{"Code":null,"RegCode":null}
我把code写死还是返回null为什么。
public HttpResponseMessage CheckMobile(string str)
{
ReturnNode a = new ReturnNode();
a.Code = "1111";

return this.seria(a);
}
public HttpResponseMessage seria(ReturnNode aa)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string str = serializer.Serialize(aa);
return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") };
}
...全文
320 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
bindao2010 2015-11-19
  • 打赏
  • 举报
回复
http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-1#sending_simple_types
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 18 楼 linyingnan 的回复:
[quote=引用 17 楼 u011498721 的回复:] [quote=引用 15 楼 linyingnan 的回复:] [quote=引用 14 楼 u011498721 的回复:] [quote=引用 13 楼 linyingnan 的回复:] [quote=引用 11 楼 u011498721 的回复:]方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的[/quote]好像是我post的网址问题。。。是不是post的时候网址也得加上?str=?,但是加上好像又没参数什么事儿了[/quote] 如果你定义的是[Frombody]不用加,如果没有就表示是URL上面传过来的 例如: http://localhost/api/sample[/quote] 那如果是url传过去的。。那那个post方法的参数有什么意义么[/quote][yabao=][/yabao] 这个基于你自己的需求,例如你在POST的同时还想接受某些参数,虽然很少人会这么做 ^_^[/quote] 谢谢了,我原来以为只有Get的时候后面才加?str=?的,而post不加的。。
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 17 楼 u011498721 的回复:
[quote=引用 15 楼 linyingnan 的回复:] [quote=引用 14 楼 u011498721 的回复:] [quote=引用 13 楼 linyingnan 的回复:] [quote=引用 11 楼 u011498721 的回复:]方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的[/quote]好像是我post的网址问题。。。是不是post的时候网址也得加上?str=?,但是加上好像又没参数什么事儿了[/quote] 如果你定义的是[Frombody]不用加,如果没有就表示是URL上面传过来的 例如: http://localhost/api/sample[/quote] 那如果是url传过去的。。那那个post方法的参数有什么意义么[/quote][yabao=][/yabao] 这个基于你自己的需求,例如你在POST的同时还想接受某些参数,虽然很少人会这么做 ^_^
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 15 楼 linyingnan 的回复:
[quote=引用 14 楼 u011498721 的回复:] [quote=引用 13 楼 linyingnan 的回复:] [quote=引用 11 楼 u011498721 的回复:]方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的[/quote]好像是我post的网址问题。。。是不是post的时候网址也得加上?str=?,但是加上好像又没参数什么事儿了[/quote] 如果你定义的是[Frombody]不用加,如果没有就表示是URL上面传过来的 例如: http://localhost/api/sample[/quote] 那如果是url传过去的。。那那个post方法的参数有什么意义么
QQ234788028 2014-10-21
  • 打赏
  • 举报
回复
熟练使用POST模拟登录 模拟表单提交 模拟投票 模拟灌水
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 14 楼 u011498721 的回复:
[quote=引用 13 楼 linyingnan 的回复:] [quote=引用 11 楼 u011498721 的回复:]方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的[/quote]好像是我post的网址问题。。。是不是post的时候网址也得加上?str=?,但是加上好像又没参数什么事儿了[/quote] 如果你定义的是[Frombody]不用加,如果没有就表示是URL上面传过来的 例如: http://localhost/api/sample
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 13 楼 linyingnan 的回复:
[quote=引用 11 楼 u011498721 的回复:]方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的[/quote]好像是我post的网址问题。。。是不是post的时候网址也得加上?str=?,但是加上好像又没参数什么事儿了
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 11 楼 u011498721 的回复:
方法在楼上
这段代码没有问题,你调试下,看看输入的HttpResponseMessage是什么的
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 9 楼 linyingnan 的回复:
[quote=引用 8 楼 u011498721 的回复:] [quote=引用 7 楼 linyingnan 的回复:] [quote=引用 6 楼 u011498721 的回复:] [quote=引用 5 楼 linyingnan 的回复:] [quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }[/quote] 这样子写没有问题,你调试下看看str输出的是什么?[/quote]这跟那个str有关系么。。随便字符串不就行了[/quote] 用下面的code跑没有问题,看看你Post方法的定义呗
 [HttpPost]
        public HttpResponseMessage Post([FromBody]string str)
        {
            ReturnNode a = new ReturnNode();
            a.Code = "1111";

            return this.seria(a);
        }
        private HttpResponseMessage seria(ReturnNode aa)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string str = serializer.Serialize(aa);
            return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") };
        }
[/quote] 会不会是电脑少装了什么东西
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 9 楼 linyingnan 的回复:
[quote=引用 8 楼 u011498721 的回复:] [quote=引用 7 楼 linyingnan 的回复:] [quote=引用 6 楼 u011498721 的回复:] [quote=引用 5 楼 linyingnan 的回复:] [quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }[/quote] 这样子写没有问题,你调试下看看str输出的是什么?[/quote]这跟那个str有关系么。。随便字符串不就行了[/quote] 用下面的code跑没有问题,看看你Post方法的定义呗
 [HttpPost]
        public HttpResponseMessage Post([FromBody]string str)
        {
            ReturnNode a = new ReturnNode();
            a.Code = "1111";

            return this.seria(a);
        }
        private HttpResponseMessage seria(ReturnNode aa)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string str = serializer.Serialize(aa);
            return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") };
        }
[/quote] 方法在楼上
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
public static string Post(string PostUrl, string Parameters) { string content = string.Empty; try { //转换为字节数组 byte[] bytesRequestData = Encoding.UTF8.GetBytes(Parameters); //path不是登录界面,是登录界面向服务器提交数据的界面 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(PostUrl); myReq.Method = "post"; myReq.ContentType = "application/x-www-form-urlencoded"; //填充POST数据 myReq.ContentLength = bytesRequestData.Length; Stream requestStream = myReq.GetRequestStream(); //requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Close(); //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); //获取服务器返回信息 Stream myStream = HttpWResp.GetResponseStream(); StreamReader reader = new StreamReader(myStream, Encoding.UTF8); content = reader.ReadToEnd(); reader.Close(); HttpWResp.Close(); } catch (Exception ex) { content = ex.ToString(); } return content; } 网上找的
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 8 楼 u011498721 的回复:
[quote=引用 7 楼 linyingnan 的回复:] [quote=引用 6 楼 u011498721 的回复:] [quote=引用 5 楼 linyingnan 的回复:] [quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }[/quote] 这样子写没有问题,你调试下看看str输出的是什么?[/quote]这跟那个str有关系么。。随便字符串不就行了[/quote] 用下面的code跑没有问题,看看你Post方法的定义呗
 [HttpPost]
        public HttpResponseMessage Post([FromBody]string str)
        {
            ReturnNode a = new ReturnNode();
            a.Code = "1111";

            return this.seria(a);
        }
        private HttpResponseMessage seria(ReturnNode aa)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string str = serializer.Serialize(aa);
            return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") };
        }
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 7 楼 linyingnan 的回复:
[quote=引用 6 楼 u011498721 的回复:] [quote=引用 5 楼 linyingnan 的回复:] [quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }[/quote] 这样子写没有问题,你调试下看看str输出的是什么?[/quote]这跟那个str有关系么。。随便字符串不就行了
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 6 楼 u011498721 的回复:
[quote=引用 5 楼 linyingnan 的回复:] [quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }[/quote] 这样子写没有问题,你调试下看看str输出的是什么?
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 5 楼 linyingnan 的回复:
[quote=引用 3 楼 u011498721 的回复:] [quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}[/quote] returnNode是个类就是下面这样弄得不对么 public class ReturnNode { private string code; private string regCode; //private string data; public string Code { get { return code; } set { code = value; } } public string RegCode { get { return regCode; } set { regCode = value; } } }
林英南 2014-10-21
  • 打赏
  • 举报
回复
引用 3 楼 u011498721 的回复:
[quote=引用 2 楼 devmiao 的回复:] a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。[/quote] public string Code {get;set;}
林英南 2014-10-21
  • 打赏
  • 举报
回复
调试看你str是不是为空,如果为空,看看类ReturnNode里面Code的定义式什么
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
引用 2 楼 devmiao 的回复:
a.Code要定义成属性而不是字段
我好晕啊。。该怎么定义啊。。
devmiao 2014-10-21
  • 打赏
  • 举报
回复
a.Code要定义成属性而不是字段
呆瓜丶 2014-10-21
  • 打赏
  • 举报
回复
public HttpResponseMessage CheckMobile(string str) { ReturnNode a = new ReturnNode(); a.Code = "1111"; return this.seria(a); } public HttpResponseMessage seria(ReturnNode aa) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string str = serializer.Serialize(aa); return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") }; }

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧