2016-07-29 29 views
0

我创建了一个REST API,但是当我用GET调用它,它返回:REST API响应包括“这个XML文件没有出现......”

This XML file does not appear to have any style information associated with it. The document tree is shown below. 

<ArrayOfgetSiteList xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Thor"> 
<getSiteList> 
<responseCode>1</responseCode> 
<responseMessage>No message</responseMessage> 
<sites> 
<Sites> 
<siteAddress>11 Main Street054656</siteAddress> 
<siteId>1</siteId> 
<siteName>Sky Towers</siteName> 
</Sites> 
<Sites> 
<siteAddress>1 Grande Street</siteAddress> 
<siteId>2</siteId> 
<siteName>Hotel Grande</siteName> 
</Sites> 
</sites> 
</getSiteList> 
</ArrayOfgetSiteList> 

我如何得到它只返回方括号内的JSON文本,而不是“This XML file ...”部分?是否有一些标题我应该添加到响应中,如果有的话,我在哪里添加它?

此外,我怎样才能得到它格式化/像http://echo.jsontest.com/key/value/mykey/myvalue/anotherkey/anothervalue很好地缩进?

感谢您的帮助。

+1

这不是xml ...这是JSON。你究竟如何输出这个json?如果它像'header('Content-type:text/xml');回声json_encode($ arr);',那么你得到了你告诉系统要做的事情:输出json和谎言,它是xml。 –

+0

这是因为XML是JSON ... – War10ck

+0

我没有故意添加任何标题。我遵循http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms并输出JSON作为列表。我应该在哪里添加标题?当我在Postman上运行它时,它给了我正在寻找的JSON输出。但是当我在网络浏览器(Chrome)上运行它时,会出现上述输出。 – Windhoek

回答

0

而不是返回PartialView()您应该返回Json()作为您的ActionResult

return Json(someItem,JsonRequestBehavior.AllowGet);   
+0

这是针对语言不可知论问题的极其框架的答案。 – Evert

+0

谢谢。我使用C#输出JSON。而我的API控制器正在返回一个字符串。我的问题是:除了方括号内的内容,我应该在字符串中放置什么? – Windhoek