1

.Net Web APIを使用して、サードパーティがサーバーに対して行ったRESTful呼び出しを消費するサービスを作成する必要があります。送信されるXMLを制御することはできず、とにかく変更することはできません。HTTP POST呼び出しのみを受信し、それらはすべてXML形式になります。

受信するXMLメッセージの例を次に示します。

<listing-confirmation>
<account-id>123456</account-id>
<allow-bid>true</allow-bid>
<allow-both>true</allow-both>
<allow-buy>true</allow-buy>
<allow-offers>false</allow-offers>
<auction-work-order>1234</auction-work-order>
<bid-count>1</bid-count>
<bid-increment>50</bid-increment>
<buyer-group-name>og_name bg_name</buyer-group-name>
<buyer-group-type>GlobalOpen</buyer-group-type>
<buy-now-price>1000</buy-now-price>
<condition-report-url>some_url</condition-report-url>
<current-bid>700</current-bid>
<end-timestamp>Wed May 25 21:48:09 +0000 2011</end-timestamp>
<event-sale-id>1234</event-sale-id>
<event-sale-name>event_sale_name</event-sale-name>
<facilitated-auction-code>abc</facilitated-auction-code>
<floor-price>700</floor-price>
<listing-activated-timestamp>Tue May 24 21:38:09 +0000 2011</listing-activated-timestamp>
<manheim-group-code>mgc</manheim-group-code>
<physical-location>at_auction</physical-location>
<seller-id>5000000</seller-id>
<starting-bid-price>300</starting-bid-price>
<start-timestamp>Tue May 24 21:38:09 +0000 2011</start-timestamp>
<stock-number>3BBBDOC</stock-number>
<unique-bidder-count>1</unique-bidder-count>
<vehicle-detail-url>http://localhost/vdp/show/169</vehicle-detail-url>
<view-count>0</view-count>
<vin>12345678901234567</vin>
</listing-confirmation>

いくつか質問があります。

1-ルートXMLメッセージのダッシュを処理するにはどうすればよいですか?こんな感じでしょうか

[XmlRoot("listing-confirmation")]
public class Listing_confirmation

2-xml要素のダッシュを処理するにはどうすればよいですか?これを使用しますか

[XmlElement("account-id")]
public int account_id { get; set; }

3-日時のフォーマットを処理するにはどうすればよいですか?それは自動的にそれを理解してDateTimeに変換しますか、それともそれを処理するために何かをする必要がありますか?もしそうなら、どうすればいいですか?

ありがとう!ジェレミー

2012年10月1日4時56分EST時点で編集

これが私のListing_confirmationモデルの新しいクラスファイルです

[DataContract(Name="listing-confirmation")]
public class Listing_confirmation
{
    [DataMember(Name="account-id")]
    public int account_id { get; set; }
    [DataMember(Name="allow-bid")]
    public bool allow_bid { get; set; }
    [DataMember(Name="allow-both")]
    public bool allow_both { get; set; }
    [DataMember(Name="allow-buy")]
    public bool allow_buy { get; set; }
    [DataMember(Name="allow-offers")]
    public bool allow_offers { get; set; }
    [DataMember(Name="auction-work-order")]
    public int? auction_work_order { get; set; }
    [DataMember(Name="bid-count")]
    public int? bid_count { get; set; }
    [DataMember(Name="bid-increment")]
    public int? bid_increment { get; set; }
    [DataMember(Name="buyer-group-name")]
    public string buyer_group_name { get; set; }
    [DataMember(Name="buyer-group-type")]
    public string buyer_group_type { get; set; }
    [DataMember(Name="buy-now-price")]
    public int buy_now_price { get; set; }
    [DataMember(Name="condition-report-url")]
    public string condition_report_url { get; set; }
    [DataMember(Name="current-bid")]
    public int? current_bid { get; set; }
    //[DataMember(Name="end-timestamp")]
    //public DateTime end_timestamp { get; set; }
    [DataMember(Name="event-sale-id")]
    public int? event_sale_id { get; set; }
    [DataMember(Name="event-sale-name")]
    public string event_sale_name { get; set; }
    [DataMember(Name="facilitated-auction-code")]
    public string facilitated_auction_code { get; set; }
    [DataMember(Name="floor-price")]
    public int floor_price { get; set; }
    //[DataMember(Name="listing-activated-timestamp")]
    //public DateTime listing_activated_timestamp { get; set; }
    [DataMember(Name="manheim-group-code")]
    public string manheim_group_code { get; set; }
    //[DataMember(Name="message-triggered")]
    //public DateTime message_triggered { get; set; }
    [DataMember(Name="physical-location")]
    public string physical_location { get; set; }
    [DataMember(Name="seller-id")]
    public int seller_id { get; set; }
    [DataMember(Name="starting-bid-price")]
    public int? starting_bid_price { get; set; }
    //[DataMember(Name="start-timestamp")]
    //public DateTime start_timestamp { get; set; }
    [DataMember(Name="stock-number")]
    public string stock_number { get; set; }
    [DataMember(Name="unique-bidder-count")]
    public int? unique_bidder_count { get; set; }
    [DataMember(Name="vehicle-detail-url")]
    public string vehicle_detail_url { get; set; }
    [DataMember(Name="view-count")]
    public int? view_count { get; set; }
    [DataMember(Name="vin")]
    public string vin { get; set; }
}

ただし、それでもデータを正しくバインドしません。コントローラをデバッグすると、常にModelState.IsValid = falseになり、Listing_Confirmationオブジェクトは常にnullになります。誰か助けてもらえますか?

ありがとう!

4

3 に答える 3

1

WebAPIがパラメータバインディングを機能させる方法から:

パラメータをバインドするには、モデルバインディングとフォーマッタの2つの手法があります。実際には、WebAPIはモデルバインディングを使用してクエリ文字列から読み取り、フォーマッターを使用して本文から読み取ります。

サードパーティから受信しているXMLがPOST操作の本体にある場合は、カスタムXMLメディアフォーマッターを記述して、XMLを取得し、受信しているデータを表す.NETオブジェクトを返すモデルバインディングを実行する必要があります。

カスタムメディアタイプフォーマッタを作成する方法の例については、メディアフォーマッタを参照してください。デフォルトのXMLメディアタイプフォーマッターをカスタムXMLフォーマッターに置き換える必要があります。

于 2012-10-01T19:53:14.800 に答える
0

私はついに問題が何であるかを理解しました。

1-Application_Start()メソッドに、DataContractSerializerの代わりにXmlSerializerを使用するように指定する次のコード行を追加しました。

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

2-XmlRoot属性とXmlElement属性を使用して、クラス名と各プロパティを装飾しました。これが私の新しいクラスファイルです-

[XmlRoot("listing-confirmation")]
public class Listing_confirmation
{
    [XmlElement("account-id")]
    public int account_id { get; set; }
    [XmlElement("allow-bid")]
    public bool allow_bid { get; set; }
    [XmlElement("allow-both")]
    public bool allow_both { get; set; }
    [XmlElement("allow-buy")]
    public bool allow_buy { get; set; }
    [XmlElement("allow-offers")]
    public bool allow_offers { get; set; }
    [XmlElement("auction-work-order")]
    public int auction_work_order { get; set; }
    [XmlElement("bid-count")]
    public int bid_count { get; set; }
    [XmlElement("bid-increment")]
    public int bid_increment { get; set; }
    [XmlElement("buyer-group-name")]
    public string buyer_group_name { get; set; }
    [XmlElement("buyer-group-type")]
    public string buyer_group_type { get; set; }
    [XmlElement("buy-now-price")]
    public int buy_now_price { get; set; }
    [XmlElement("condition-report-url")]
    public string condition_report_url { get; set; }
    [XmlElement("current-bid")]
    public int current_bid { get; set; }
    //[XmlElement("end-timestamp")]
    //public DateTime end_timestamp { get; set; }
    [XmlElement("event-sale-id")]
    public int event_sale_id { get; set; }
    [XmlElement("event-sale-name")]
    public string event_sale_name { get; set; }
    [XmlElement("facilitated-auction-code")]
    public string facilitated_auction_code { get; set; }
    [XmlElement("floor-price")]
    public int floor_price { get; set; }
    //[XmlElement("listing-activated-timestamp")]
    //public DateTime listing_activated_timestamp { get; set; }
    [XmlElement("manheim-group-code")]
    public string manheim_group_code { get; set; }
    //[XmlElement("message-triggered")]
    //public DateTime message_triggered { get; set; }
    [XmlElement("physical-location")]
    public string physical_location { get; set; }
    [XmlElement("seller-id")]
    public int seller_id { get; set; }
    [XmlElement("starting-bid-price")]
    public int starting_bid_price { get; set; }
    //[XmlElement("start-timestamp")]
    //public DateTime start_timestamp { get; set; }
    [XmlElement("stock-number")]
    public string stock_number { get; set; }
    [XmlElement("unique-bidder-count")]
    public int unique_bidder_count { get; set; }
    [XmlElement("vehicle-detail-url")]
    public string vehicle_detail_url { get; set; }
    [XmlElement("view-count")]
    public int view_count { get; set; }
    [XmlElement("vin")]
    public string vin { get; set; }
}

3-渡された値を有効なDateTimeに変換できなかったため、すべてのDateTimeプロパティをコメントアウトする必要がありました。私はまだそれを正しく機能させるために取り組んでいます。

于 2012-10-01T21:13:42.543 に答える
0

私はついにDateTimeフォーマットの問題も理解することができました。これは私が1つのDateTimeをどのように処理したかの例です。私は他の人も同じように扱いました。

[XmlIgnore]
    public DateTime? start_timestamp { get; set; }
    [XmlElement("start-timestamp")]
    public string start_timestampstring
    {
        get
        {
            return start_timestamp.HasValue ? start_timestamp.Value.ToString("ddd MMM dd HH:mm:ss zzz yyyy") : string.Empty;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    start_timestamp = DateTime.ParseExact(value, "ddd MMM dd HH:mm:ss zzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
                }
                catch
                {
                    start_timestamp = null;
                }

            }
            else
            {
                start_timestamp = null;
            }
        }
    }
于 2012-10-01T21:30:46.107 に答える