简体   繁体   English

如何在WCF Rest中传递Json对象?

[英]How to pass Json object in WCF Rest?

I use WCF Rest 40(CS) Template for my REST project. 我将WCF Rest 40(CS)模板用于REST项目。
I didn't get any problem when passing a data through URL: 通过URL传递数据时没有遇到任何问题:

http://localhost:8525/Device/Login?deviceID=testid&password=a&serialNum=testserial  

But when passing Json data, I get: 但是当传递Json数据时,我得到:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 17 Feb 2012 08:41:38 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 1647
Set-Cookie: ASP.NET_SessionId=4tez22jvuy0s3tiioctukvdq; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for more details.</p>
    </div>
  </body>
</html>  

Method not Allowed 方法不允许
(Response using Fiddler) (使用Fiddler进行响应)

In side service: 在边服务:

namespace WCFRest.Rest
{
    [ServiceContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DeviceComponent
    {
        [WebInvoke(UriTemplate = "Login?deviceID={deviceID}&password={password}&serialNum={serialNum}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public WRModel.Entities.Session Login(string deviceID, string password, string serialNum)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>();
            return svc.Login(deviceID, password, serialNum, ip);
        }

        [WebInvoke(UriTemplate = "Logout", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public bool Logout(WRModel.Entities.DeviceSession deviceSession)
        {
            WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>();
            return svc.Logout(deviceSession);
        }
    }
}  

The Login part works fine, but the Logout causes error. Login部分工作正常,但Logout导致错误。 Where did I go wrong? 我哪里做错了? Thanks for help. 感谢帮助。 Here some image using fiddler: 这里有一些使用fiddler的图片:

在此输入图像描述

EDIT 编辑

DeviceSession Entity DeviceSession实体

[DataContract(Name = "DeviceSession", Namespace = "http://MySite.com/1.0/DeviceSession")]
public class DeviceSession : IDeviceSession
{
public DeviceSession()
{
}

public DeviceSession(string session, string loginID, string serial)
{
    Session = session;
    LoginID = loginID;
    Serial = serial;
}

string _session;

[DataMember(Order = 0)]
public string Session
{
    get { return _session; }
    set { _session = value; }
}

string _LoginID;

[DataMember(Order = 1)]
public string LoginID
{
    get { return _LoginID; }
    set { _LoginID = value; }
}

string _serial;

[DataMember(Order = 2)]
public string Serial
{
    get { return _serial; }
    set { _serial = value; }
}

} }

You have actually done slightly different things in each example. 在每个例子中,你实际上做了一些不同的事情。 In the first example (login) you are posting variables on the url, in the second you are posting in the body. 在第一个示例(登录)中,您要在url上发布变量,在第二个示例中,您要在正文中发布。

Whats important to note is that the JSON you need to post in the second example is the json for string, not the json for an object containing a string. 需要注意的是,您需要在第二个示例中发布的JSON是字符串的json,而不是包含字符串的对象的json。 This means that the json isnt valid for the type string. 这意味着json对类型字符串无效。

What i would recommend is to wrap it in an object or call it on the query string as in your first example. 我建议将其包装在一个对象中,或者像第一个示例一样在查询字符串上调用它。

For example you could use 例如,您可以使用

 [WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
 public WRModel.Entities.Session Login(LoginDetails details)

Where login details is a class containing deviceId Password serialNumber 登录详细信息是包含deviceId Password serialNumber的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM