一个ApiController可以返回一个对象与其他对象的集合? [英] Can an ApiController return an object with a collection of other objects?

查看:751
本文介绍了一个ApiController可以返回一个对象与其他对象的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个客户类,只是一些简单的属性(如名称等),那么我可以创建一个 CustomersController ApiController 派生,而查询使用URL / API我的客户对象/客户

同样,如果我有一个简单的属性(说明量的订单),那么我可以创建一个 OrdersController 和访问它以同样的方式。

但是,如果我再属性添加到我的客户类,列出了订单 S采用了相关的客户

 公共类客户
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    // ...
    公共虚拟的ICollection<排序>订单{搞定;组; }
}

...然后当我尝试使用 / API /客户查询我得到一个500服务器错误。我可以把一个断点 CustomersController 内可以看到,它调用正确的方法,并试图返回一个值。然而,在某个框架的胆量它未能将其转换成HTTP响应。

看起来它不能序列化订单信息和/或包装才能进行HTTP响应,但我不知道我需要做的,使这个发生。还是纯粹的东西的WebAPI的目的不是返回复杂对象的情况?


更新:我的code是在这里:的https://gist.github .COM / 43ed2f29f8adfb44cef6


更新2 :更多的错误的信息,我得到:


 服务器错误
在检索HTTP网站时遇到错误://本地主机1031 / API /客户/ 6。


  
  

这可能是停机维护
  或配置不正确。
      下面是一些建议:
      稍后重新载入此网页。
      HTTP错误500(内部服务器错误):一个意外的情况,而服务器尝试执行时遇到
  请求。


本的IntelliTrace日志显示如下: http://i.imgur.com/iHxJl.png

[我简直不能相信的IntelliTrace窗口不允许我复制的文本,让我不得不实际打印屏幕和上传图片。今年是哪一年?]


相关报道:<一href=\"http://stackoverflow.com/questions/10897523/asp-net-web-api-showing-correctly-in-vs-but-giving-http500\">ASP.Net网页API显示正确VS但给人HTTP500


解决方案

正在传递一个实体框架的实体,这将导致问题。你需要的数据传输对象传递和接收数据。因为你绑定在一起的实体,它不会让你序列化的数据。

If I have a Customer class that just has simple properties (e.g. Name, etc.) then I can create a CustomersController that derives from ApiController, and query my Customer objects using the URL /api/customers.

Similarly, if I have an Order class with simple properties (Description, Amount) then I can create an OrdersController and access it in the same way.

However, if I then add a property to my Customer class that lists the Orders associated with that Customer:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    // ...
    public virtual ICollection<Order> Orders { get; set; }
}

...then when I try to query using /api/customers I get a 500 Server Error. I can put a breakpoint within the CustomersController and can see that it does call the correct method, and attempts to return a value. However, somewhere in the guts of the framework it fails to turn that into an HTTP response.

It looks like it cannot serialize the Orders information and/or package it up for the HTTP response, but I'm not sure what I need to do to make this happen. Or is it simply the case that the WebApi stuff is not designed to return complex objects?


Update: my code is here: https://gist.github.com/43ed2f29f8adfb44cef6


Update 2: more information on the error I get:

Server error
The website encountered an error while retrieving http://localhost:1031/api/customers/6.

It may be down for maintenance or configured incorrectly. Here are some suggestions: Reload this webpage later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

The Intellitrace log shows the following: http://i.imgur.com/iHxJl.png

[I can hardly believe that the Intellitrace window does not allow me to copy the text, so that I had to actually print-screen and upload an image. What year is this?]


Related: ASP.Net Web API showing correctly in VS but giving HTTP500

解决方案

You are passing an Entity Framework entity which will cause problems. You need Data Transfer Objects to pass and receive your data. Because you bind entities together, it won't allow you to serialize the data.

这篇关于一个ApiController可以返回一个对象与其他对象的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆