2015-11-04 44 views
0

我有被配置爲JSON格式如下XML響應

config.Formatters.JsonFormatter.SerializerSettings.Formatting = 
       Newtonsoft.Json.Formatting.Indented; 

總是回覆結果的網頁API的解決方案,但現在我有一個要求,只有API的一個使用XML電話迴應響應。但是,如果我將XML格式化程序添加到系統中,則所有API調用都會受到影響

config.Formatters.XmlFormatter.UseXmlSerializer = true; 

然後所有的API調用都會受到影響。

我有一個硬編碼的XML字符串,我想給作爲響應。 我該如何解決這個問題?

+0

http://stackoverflow.com/questions/14313241/per-route-formatter-configuration-in-web的可能的複製-api – torvin

回答

0

您必須在api級別使用Сonfiguration.Formatters.XmlFormatter。嘗試下面的代碼

public IHttpActionResult ApiMethod() 
{ 
    ... 
    return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter); 
} 
+0

我使用的是相同的 –

+0

但根據你的問題描述,似乎你已經把Formatter放到了Global.asax中。它必須採取行動。 –

0

寫下面的代碼在你的行動:

 Class1 c1 = new Class1();//Your Model class 
     var content = new ObjectContent<Class1>(c1, 
     GlobalConfiguration.Configuration.Formatters.XmlFormatter); 
     return new HttpResponseMessage() 
     { 
      Content = content 
     };