简体   繁体   English

.Net Core Web API序列化问题

[英].Net Core Web API serialization issue

This is an excerpt from a straightforward API controller: 这是一个简单的API控制器的摘录:

[Route("api/cities")]
public class CitiesController : BaseController
{
    internal protected ICityRepository _cityRepository;
    public CitiesController(ICityRepository cityRepository) : base()
    {
        _cityRepository = cityRepository;
    }

    // GET: api/Cities
    [HttpGet]
    public IEnumerable<City> Get()
    {
        var cities = _cityRepository.GetAll();
        return cities;
    }

    // GET: api/Cities/5
    [HttpGet("{id}", Name = "GetCity")]
    public IActionResult Get(Guid id)
    {
        City city = _cityRepository.Get(id);
        if (city == null)
        {
            return NotFound();
        }

        return new ObjectResult(city);
    }

BaseController does nothing more than inherit from Controller at the moment. BaseController只是暂时从Controller继承。 Whenever I call api/cities or api/cities/E4477C67-894E-492C-95DE-001DC73730A1 I get something like this in return: 每当我打电话给api / cities或api / cities / E4477C67-894E-492C-95DE-001DC73730A1时,我会得到这样的回报:

{
    "$id": "2828",
    "$values": [
    {
        "$id": "2829"
    },
    {
        "$id": "2830"
    },
    {
        "$id": "2831"
    },
    ...

and

{
    "$id": "2827"
}

respectively. 分别。 It looks like it's returning a sequence number of a serialized object or something. 看起来它正在返回序列化对象的序列号或其他东西。 After some Googling I came across the JsonObject attribute which, by adding it to my base class, made the objects get serialized in some sort of fashion. 在一些谷歌搜索后,我遇到了JsonObject属性,通过将其添加到我的基类,使对象以某种方式被序列化。 Mind the "some sort of fashion". 注意“某种时尚”。

The response for a basic get looks a bit like this: 基本get的响应看起来有点像这样:

{
"$id": "2",
"$values": [
    {
        "$id": "3",
        "name": "Beigem",
        "zipCode": "1852",
        "id": "e4477c67-894e-492c-95de-001dc73730a1",
        "concurrencyStamp": "AAAAAAAAZxE=",
        "created": "2017-11-06T08:22:19.9733333",
        "createdBy": null,
        "modified": "2017-11-06T08:22:19.9733333",
        "modifiedBy": null,
        "isNew": false,
        "__Key": "e4477c67-894e-492c-95de-001dc73730a1"
    },
    ...

Still with the $id and $values things. 还有$ id和$值的东西。 I don't want that. 我不希望这样。 I want it to be a clean json result, not the pear-shaped one like this. 我希望它是一个干净的json结果,而不是像这样的梨形。

So there's 2 questions, basically: 所以基本上有两个问题:

  1. Why do I need to add the JsonObject attribute? 为什么我需要添加JsonObject属性? I don't see it in any other example. 我没有在任何其他例子中看到它。
  2. What's up with the weird formatting. 怎么了奇怪的格式化。 I cannot find any reference to that either... 我也找不到任何参考......

TIA! TIA!

As a rule you should not serialize EF entities. 通常,您不应序列化EF实体。

Consider defining data contracts for your controllers and return only data that is required by design of API call. 考虑为控制器定义数据协定,并仅返回API调用设计所需的数据。 With such approach you'll get EF entity from your repository and copy relevant fields to output data object: 使用这种方法,您将从存储库中获取EF实体,并将相关字段复制到输出数据对象:

// GET: api/Cities/5
[HttpGet("{id}", Name = "GetCity")]
public IActionResult Get(Guid id)
{
    City city = _cityRepository.Get(id);
    if (city == null)
    {
        return NotFound();
    }

    CityData cityData = new CityData
    {
        Name = city.Name,
        ...
    };

    return Json(cityData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM