我有一个员工表,我希望以XML格式返回浏览器。我在这里按照教程" http://csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-and-sql-server.html"并以同样的方式做到了这一点。但是,当我运行它时,我根本没有得到XML列表。
我把它放到浏览器地址栏中: 雇员/ GETALL
{CONTROLLER CODE}
// GET: ALL Employee
public List<DimEmployee> GetAll()
{
using (AdventureWorks_MBDEV_DW2008Entities entities = new AdventureWorks_MBDEV_DW2008Entities())
{
return entities.DimEmployees.ToList();
}
}
我在浏览器窗口中返回而不是XML或JSON: System.Collections.Generic.List`1 [AdventureWorksDataAccess.DimEmployee]
我看过两个教程,它返回XML,但由于某些原因不适合我。我正在使用MVC5 Web API;谁能告诉我为什么这不起作用?
感谢。
答案 0 :(得分:2)
您可能希望将功能更新为
[HttpGet]
public JsonResult GetAll()
{
using (AdventureWorks_MBDEV_DW2008Entities entities = new AdventureWorks_MBDEV_DW2008Entities())
{
return Json(entities.DimEmployees.ToList(), JsonRequestBehavior.AllowGet);
}
}
这应该处理错误的返回值。