为什么RestSharp不会将结果解析为对象?

时间:2017-12-07 14:50:04

标签: c# asp.net-core restsharp

服务器端:Asp.Net Core MVC 2
客户1方:核心2 客户端2:.Net Framework 4.6.2

两个客户端都使用相同的cs文件。

我看到response.Content包含服务器的有效响应,但response.Datanull而不是DbLibraryDto实例。

为什么会发生这种情况?如何解决?

客户方:

var client = new RestClient("http://localhost:22170/DbLibrary/AddLibrary");
      var request = new RestRequest(Method.POST);
      request.AddHeader("Cache-Control", "no-cache");
      request.AddHeader("Content-Type", "application/json");

      var dto = new DbLibraryDto()
      {
        Id = null,
        Name = "Foo",
        RowVersion = new byte[] { },
        Companies = new List<DbDepartmentDto>(),
        Users = new List<DbUserDto>()
      };

      request.AddJsonBody(dto);

      var response = client.Execute<DbLibraryDto>(request);

      var content = response.Content; // {"id":"92924078-820f-401a-55c8-08d53d80728c","name":"Foo","companies":[],"users":[],"rowVersion":"AAAAAAAAB/I="}
      var obj = response.Data; // null

控制器代码:

 [HttpPost("AddLibrary")]
    public async Task<IActionResult> AddLibrary([FromBody]DbLibraryDto dto)
    {
      if (!ModelState.IsValid) return BadRequest(ModelState);

      var obj = await _libraryService.AddLibrary(dto);
      if (obj != null) return Ok(obj);
      else return Forbid();
    }

enter image description here

UPD

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ThprServer.Dto.Common
{
  [Serializable]
  public class DbLibraryDto
  {
    public Guid? Id { get; set; }
    public string Name { get; set; }
    public List<DbDepartmentDto> Companies { get; set; } = new List<DbDepartmentDto>();
    public List<DbUserDto> Users { get; set; } = new List<DbUserDto>();
    public byte[] RowVersion { get; set; }
  }
}

0 个答案:

没有答案