尝试使用 RestKit 从 iOS 调用 ASP.NET 4 Web API,但序列化的 json 对象在服务器命令上没有得到识别。任何人都可以帮忙吗?
obj C 类;
#import "NoddyModel.h"
@synthesize messageId;
@synthesize message;
@end
(以上是NSStrings)
来自 mvc 服务的等效 C#;
using Core.MessageDocuments;
namespace WebApi.Models
{
[DataContract]
public class NoddysModel : List<NoddyModel>
{
[DataMember(IsRequired = true)]
public long NumberOfMessages
{
get { return Count; }
}
}
[DataContract]
public class NoddyModel
{
public NoddyModel(string MessageId, string Message)
{
MessageId = MessageId;
Message = Message;
}
[DataMember]
public string MessageId { get; set; }
[DataMember]
public string Message { get; set; }
}
}
和 mvc 命令;
// POST api/Noddy
[HttpPost]
public MessagesPostResponse Post(NoddyModel noddy)
{
int count = 999;
try
{
count = noddy.Message.Length + 9000;
}
catch (Exception e)
{
FileStream traceLog = new FileStream("C:\\logs\\Traces.svclog", FileMode.Append);
TextWriterTraceListener myListener = new TextWriterTraceListener(traceLog);
Trace.Listeners.Add(myListener);
myListener.WriteLine("Exception in POST NoddyModel calc count = " + e.Message + " response = " + HttpContext.Current.Request.Params.ToString());
Trace.Flush();
myListener.Flush();
traceLog.Close();
return new MessagesPostResponse(false, 0);
}
try
{
FileStream traceLog = new FileStream("C:\\logs\\Traces.svclog", FileMode.Append);
TextWriterTraceListener myListener = new TextWriterTraceListener(traceLog);
Trace.Listeners.Add(myListener);
myListener.WriteLine("Sending trace information : Noddy = " + noddy);
Trace.Flush();
myListener.Flush();
traceLog.Close();
return new MessagesPostResponse(true, count);
}
catch (Exception e)
{
FileStream traceLog = new FileStream("C:\\logs\\Traces.svclog", FileMode.Append);
TextWriterTraceListener myListener = new TextWriterTraceListener(traceLog);
Trace.Listeners.Add(myListener);
myListener.WriteLine("Exception in POST NoddyModel = " + e.Message);
Trace.Flush();
myListener.Flush();
traceLog.Close();
return new MessagesPostResponse(false, 0);
}
}
和 Obj C 函数;
RKURL *baseURL = [RKURL URLWithString: @"http://ServerX/cirrusdev/api"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString: @"http://ServerX/cirrusdev/api"];
objectManager.client.baseURL = baseURL;
[objectManager setSerializationMIMEType:RKMIMETypeJSON];
RKObjectRouter *dynrouter = [RKObjectRouter new];
[dynrouter routeClass:[NoddyModel class] toResourcePath:@"/messages" forMethod:RKRequestMethodPOST];
objectManager.router = dynrouter;
RKObjectMapping* pmsg = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pmsg mapKeyPath: @"MessageId" toAttribute:@"messageId" ];
[pmsg mapKeyPath: @"Message" toAttribute:@"message" ];
RKObjectMapping* pmsgSerializeMapping = [pmsg inverseMapping];
[objectManager.mappingProvider setSerializationMapping:pmsgSerializeMapping forClass:[NoddyModel class]];
NoddyModel *msg = [[NoddyModel alloc]init];
msg.messageId =@"abc999";
msg.message = @"Hello Cirrus";
[objectManager postObject:msg delegate:self];
以及来自 RestKit 的日志;
> 2012-10-31 15:28:26.851 ELOI[74400:c07] D
> restkit.network:RKClient.m:437 Reachability to host '0.0.0.0'
> determined for client <RKClient: 0x76dda50>, unsuspending queue
> <RKRequestQueue: 0x76ddb90 name=(null) suspended=YES requestCount=1
> loadingCount=0/5> 2012-10-31 15:28:26.851 ELOI[74400:c07] D
> restkit.network:RKRequest.m:437 Sending asynchronous POST request to
> URL `http://ServerX/cirrusdev/api/messages`. 2012-10-31 15:28:26.852
> ELOI[74400:c07] D restkit.network:RKObjectLoader.m:374 POST or PUT
> request for source object <NoddyModel: 0x7696ba0>, serializing to MIME
> Type application/json for transport... 2012-10-31 15:28:26.853
> ELOI[74400:c07] T restkit.network:RKRequest.m:399 Prepared POST
> URLRequest '<NSMutableURLRequest
> http://ServerX/cirrusdev/api/messages>'. HTTP Headers: {
> Accept = "application/json";
> "Content-Length" = 47;
> "Content-Type" = "application/json"; }. HTTP Body: {"MessageId":"abc999","Message":"Hello Cirrus"}. 2012-10-31
> 15:28:26.855 ELOI[74400:c07] D restkit.network:RKResponse.m:198
> Proceeding with request to <NSURLRequest
> http://ServerX/cirrusdev/api/messages> 2012-10-31 15:28:31.224
> ELOI[74400:c07] D restkit.network:RKResponse.m:219 NSHTTPURLResponse
> Status Code: 200 2012-10-31 15:28:31.224 ELOI[74400:c07] D
> restkit.network:RKResponse.m:220 Headers: {
> "Cache-Control" = "no-cache";
> "Content-Length" = 74;
> "Content-Type" = "application/json; charset=utf-8";
> Date = "Wed, 31 Oct 2012 15:28:30 GMT";
> Expires = "-1";
> Pragma = "no-cache";
> Server = "Microsoft-IIS/7.5";
> "X-AspNet-Version" = "4.0.30319";
> "X-Powered-By" = "ASP.NET"; } 2012-10-31 15:28:31.225 ELOI[74400:c07] T restkit.network:RKResponse.m:231 Read response body:
> {"Successful":false,"Message":"We have received 0
> message(s)","Data":null}
PS我手动编辑了服务器名称,所以如果不同请忽略