1
嘗試從iOS使用RestKit調用ASP.NET 4 Web API,但序列化的json對象在服務器命令中未得到識別。 任何人都可以幫忙嗎?使用RestKit iOS將POST對象發佈到MVC4 webapi看不到json對象
obj C class;
#import "NoddyModel.h"
@synthesize messageId;
@synthesize message;
@end
(以上爲NSString的)自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 [email protected]"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我手動編輯服務器名稱,以便如果不同,請忽略
您在服務器上進行了哪些調試?哪些代碼塊從Web服務返回?究竟是什麼問題? –
嗨,目前它不可能讓我得到服務器跟蹤文件。但它告訴我的是,它進入Post操作,它嘗試下面的代碼(第一位)嘗試 count = noddy.Message.Length + 9000; }但報告noddy沒有實例化 - 所以我們可以看到它沒有將json數據轉移到對象中NoddyModel – stan4th
只是爲了通知您我已經解決了這個問題,明天我會發佈一個解決方案 - 當我可以確保確切的細節。 – stan4th