WCF JSON Web服务,从GET转移到POST的问题

时间:2013-07-02 13:47:52

标签: .net wcf web-services asp-classic

我的网址路径组件太长,我正在尝试将方法从GET更改为POST。我正在进行反序列化问题。这是服务

    public class AppUser : IAppUser
{
    //[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddShop/{shop}")]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate = "AddShop")]
    List<fme.AppUserResult> services.IAppUser.AddShop(string shop)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        fme.AppUser oUser = jss.Deserialize<fme.AppUser>(shop);

        return oUser.Add();         
    }       
}

这是请求(如果它不到260个字符,它可以使用GET)

{"shopCity":"City","shopState":"State","shopPhone":"Phone","shopName":"Name","shopEmail":"Email","shopHours":"Hours","shopZip":"Zip","shopAddress":"Street"}

我也试过

{"shop":{"shopCity":"City","shopState":"State","shopPhone":"Phone","shopName":"Name","shopEmail":"Email","shopHours":"Hours","shopZip":"Zip","shopAddress":"Street"}}

这是错误:

  

服务器在处理请求时遇到错误。该   异常消息是'反序列化对象时出错   键入System.String。从名称空间“'预期结束元素”root“。   从命名空间''找到元素'shopCity'。请参阅服务器日志了解更多   细节。异常堆栈跟踪是:

1 个答案:

答案 0 :(得分:2)

可能是该方法自动尝试将json反序列化为参数的类型。因为它不能将json反序列化为string类型的对象,所以它会出错。尝试将参数类型更改为 fme.AppUser ,而不是字符串

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate = "AddShop")]
List<fme.AppUserResult> services.IAppUser.AddShop(fme.AppUser oUser)
{
    return oUser.Add();         
}