0

我的 URL 路径组件太长,我正在尝试将方法从 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 类型的对象时出错。应来自命名空间“”的结束元素“根”。从命名空间 ''.' 中找到元素 'shopCity'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:

4

1 回答 1

2

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

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate = "AddShop")]
List<fme.AppUserResult> services.IAppUser.AddShop(fme.AppUser oUser)
{
    return oUser.Add();         
}  
于 2013-07-02T14:08:11.093 回答