Web API不接受XML

时间:2015-03-08 12:54:02

标签: xml asp.net-web-api

我尝试使用Web API创建Web服务,该Web服务将接受从SAP PI系统传入的XML文件。我有XML格式,但Web服务不接受它。

<?xml version="1.0" encoding="UTF-8"?>
<ns0:ContactUs xmlns:ns0="http://customdomain.com/ContactUs">
 <userId></userId>
 <firstName></firstName>
 <lastName></lastName>
 <e_mail></e_mail>
 <gsm></gsm>
 <country></country>
 <city></city>
 <subject></subject>
 <type></type>
 <detail></detail>
 <message></message>
 <orderId></orderId>
 <storeName></storeName>
</ns0:ContactUs>

我尝试使用DataContract来装饰控制器,看起来它现在接受命名空间,但我收到请求是无效的错误,说明所有字段都是必需的(在JSON BTW中),尽管它们都被发送。

我在使用原始XML文件进行测试并接受/ xml标头时使用Postman。

这是控制器POST方法和模型:

public HttpResponseMessage PostCustomerRecord(CustomerRecord customerrecord)
    {
        if (ModelState.IsValid && customerrecord != null)
        {
            db.CustomerRecords.Add(customerrecord);
            db.SaveChanges();
            try
            {
                Mailing.SendMail.Send();
            }
            catch (System.Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, customerrecord);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = customerrecord.Id }));
            return response;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }

namespace ContactUs.Models
{
[DataContract(Namespace="http://customdomain.com/ContactUs", Name="ContactUs")]
public class CustomerRecord
{
    [Key]
    public int Id { get; set; }

    [Required]
    [DataMember(Name="type")]
    [StringLength(50)]
    public string type { get; set; }

    [Required]
    [DataMember]
    [StringLength(500)]
    public string subject { get; set; }

    [Required]
    [DataMember]
    public string message { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string firstName { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string lastName { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string country { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string city { get; set; }

    [Required]
    [DataMember]
    [StringLength(20)]
    public string gsm { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string e_mail { get; set; }

    [Required]
    [DataMember]
    [StringLength(50)]
    public string detail { get; set; }

    [StringLength(50)]
    [DataMember]
    public string storeName { get; set; }

    [StringLength(50)]
    [DataMember]
    public string userID { get; set; }

    [StringLength(50)]
    [DataMember]
    public string orderID { get; set; }

}

}

0 个答案:

没有答案