2013-03-12 37 views
19

Tôi có một phương thức web Api mà nên trả về một dữ liệu xml nhưng nó sẽ trả về chuỗi:Làm thế nào để trả về dữ liệu Xml từ một phương pháp API Web?

public class HealthCheckController : ApiController 
    {  
     [HttpGet] 
     public string Index() 
     { 
      var healthCheckReport = new HealthCheckReport(); 

      return healthCheckReport.ToXml(); 
     } 
    } 

Nó trả về:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> 
<myroot><mynode></mynode></myroot> 
</string> 

và tôi đã thêm bản đồ này:

config.Routes.MapHttpRoute(
       name: "HealthCheck", 
       routeTemplate: "healthcheck", 
       defaults: new 
       { 
        controller = "HealthCheck", 
        action = "Index" 
       }); 

Cách chỉ trả lại các bit xml:

<myroot><mynode></mynode></myroot> 

Nếu tôi đã sử dụng chỉ MVC, tôi có thể được sử dụng dưới đây nhưng Web API không hỗ trợ "Nội dung":

[HttpGet] 
     public ActionResult Index() 
     { 
      var healthCheckReport = new HealthCheckReport(); 

      return Content(healthCheckReport.ToXml(), "text/xml"); 
     } 

Tôi cũng đã thêm các mã dưới đây để lớp WebApiConfig:

config.Formatters.Remove(config.Formatters.JsonFormatter); 
config.Formatters.XmlFormatter.UseXmlSerializer = true; 
+1

Bạn có thể chỉ trả lại dụ HealthCheckReport, và để cho các định dạng XML làm serialization ? Ngay bây giờ, bạn đang tuần tự hóa thành XML trong bộ điều khiển của bạn và sau đó truyền chuỗi đó tới trình định dạng XML. Sau đó, trình định dạng XML sẽ tuần tự hóa chuỗi thành XML. –

Trả lời

39

cách nhanh nhất là điều này,

public class HealthCheckController : ApiController 
{  
    [HttpGet] 
    public HttpResponseMessage Index() 
    { 
     var healthCheckReport = new HealthCheckReport(); 

     return new HttpResponseMessage() {Content = new StringContent(healthCheckReport.ToXml(), Encoding.UTF8, "application/xml")}; 
    } 
} 

nhưng nó cũng rất dễ dàng để xây dựng một lớp XmlContent mới có nguồn gốc từ HttpContent để suppor t XmlDocument hoặc XDocument trực tiếp. ví dụ.

public class XmlContent : HttpContent 
{ 
    private readonly MemoryStream _Stream = new MemoryStream(); 

    public XmlContent(XmlDocument document) { 
     document.Save(_Stream); 
      _Stream.Position = 0; 
     Headers.ContentType = new MediaTypeHeaderValue("application/xml"); 
    } 

    protected override Task SerializeToStreamAsync(Stream stream, System.Net.TransportContext context) { 

     _Stream.CopyTo(stream); 

     var tcs = new TaskCompletionSource<object>(); 
     tcs.SetResult(null); 
     return tcs.Task; 
    } 

    protected override bool TryComputeLength(out long length) { 
     length = _Stream.Length; 
     return true; 
    } 
} 

và bạn có thể sử dụng nó giống như bạn sẽ sử dụng StreamContent hoặc StringContent, ngoại trừ việc nó chấp nhận một XmlDocument,

public class HealthCheckController : ApiController 
{  
    [HttpGet] 
    public HttpResponseMessage Index() 
    { 
     var healthCheckReport = new HealthCheckReport(); 

     return new HttpResponseMessage() { 
      RequestMessage = Request, 
      Content = new XmlContent(healthCheckReport.ToXmlDocument()) }; 
    } 
} 
+0

Lớp XmlContent được sử dụng như thế nào? Nó có phải đăng ký ở đâu đó không? –

Các vấn đề liên quan