代码改变世界

一个诡异的WCF问题

2013-01-05 16:24  dreamhappy  阅读(1303)  评论(7编辑  收藏  举报

涉及到的技术概念 WCF,REST,AJAX,JSON

遇到的问题:前台页面使用ajax请求后台rest服务,get请求没有问题,post请求时,后台接收参数总是null

rest方法属性,请求参数和返回参数都是json格式

#region  创建数据库配置
        /// <summary>
        /// 创建数据库配置
        /// </summary>
        /// <param name="connectionconfig">connectionconfig表实体</param>
        /// <returns>返回受影响行数</returns>
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "CreateConnectionConfig", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
        public bool CreateConnectionConfig(ConnectionConfig connectionConfig)
        {
           ...

        }
#endregion

chome控制台的调试结果

问题表现在 connectionconfig接收值总是null

如果修改信息风格为bare 

WebMessageBodyStyle.Bare

connectionconfig不是null但是其属性是null

最终的解决方法

将json值得connectionconfig 与rest方法的参数名称connectionConfig一致,主要是大小写一致

后台就不会出现null值

原因不详