1

RestKitを使用してiOSからASP.NET4Web APIを呼び出そうとしましたが、シリアル化されたjsonオブジェクトがサーバーコマンドで認識されません。誰か助けてもらえますか?

objCクラス;

 #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);
    }
}

およびObjC関数。

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私は手動でサーバー名を編集したので、異なる場合は無視してください

4

1 に答える 1

0

試行錯誤しながら試行する必要のあることがたくさんありました。最後の問題は、ASP.NET Web API アクションが Noddy オブジェクトをインスタンス化できなかったことです。これは、パラメーターなしの初期化子が必要だったためです。

于 2012-11-06T16:20:22.063 に答える