从Api控制器检索数据时出错

时间:2018-09-07 06:11:46

标签: asp.net asp.net-web-api asp.net-core asp.net-core-webapi

我正在使用Visual Studio 2017开发ASP.NET Core Api和Xamarin表单客户端。

我遇到错误

  

System.Runtime.Serialization.SerializationException:无效的JSON字符串

因为response.Content为空,所以从API检索数据时,但是将此URL粘贴到浏览器“ https://localhost:44305/api/Agreement/GetAgreementText/1”中时,它将在浏览器中显示数据。当我使用客户端运行时,不会撞到api方法调试点。

这是我的APi方法

[HttpGet]
[Route("GetAgreementText/{id}")]
public DefaultApiResult GetAgreementText(long Id)
{
    Company com = _companyRepository.Get(Id);
    string st = com.AgreementText;

    DefaultApiResult result = new DefaultApiResult
            {
                Data = st
            };
    return result;
}

这是我的客户端应用程序Api调用方法

public string GetAgreementTextLoading(long idCompany)
{
    string agreementText = "";
    // var token = _tokenService.GetLastActivateToken().Hash;
    var clientURL = "https://localhost:44305/";
    var client = new RestClient(clientURL);
    var request = new RestRequest("api/Agreement/GetAgreementText/{Id}", Method.GET);

    request.AddUrlSegment("Id", idCompany.ToString());

    IRestResponse response = client.Execute(request);
    AppRestResponse apiResponse = SimpleJson.DeserializeObject<AppRestResponse>(response.Content);

    var statusMessage = "";

    if (apiResponse.Success)
    {
        statusMessage = "Success.";

        if (!string.IsNullOrEmpty(response.Content))
        {
            agreementText = apiResponse.Data.ToString();
        }
        else
        {
            throw new Exception("Invalid response");
        }
    }
    else
    {
        agreementText = "Error retrieving agreement text";
    }

    return agreementText;
}

public class AppRestResponse
{
    public bool Success { get; set; }
    public object Data { get; set; }
    public IEnumerable<AppRestReponseError> ErrorMessages { get; set; }
}

public class DefaultApiResult
{
        public bool Success
        {
            get
            {
                return ErrorMessages.Count == 0;
            }
            private set { }
        }

        public List<ErrorMessage> ErrorMessages { get; set; }

        public object Data { get; set; }

        public DefaultApiResult()
        {
            ErrorMessages = new List<ErrorMessage>();
        }

        public DefaultApiResult(string errorMessage)
            :this()
        {
            ErrorMessages.Add(new ErrorMessage()
            {
                Message = errorMessage
            });
        }

        public DefaultApiResult(string[] errorMessages)
            :this()
        {
            foreach (var errorMessage in errorMessages)
            {
                ErrorMessages.Add(new ErrorMessage()
                {
                    Message = errorMessage
                });
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我不确定SimpleJson和您正在使用的其他客户端。

但是,假设您使用的是RestSharp,则似乎不需要在此处使用SimpleJson反序列化响应。

我只删除了以下代码:

IRestResponse response = client.Execute(request);
AppRestResponse apiResponse = SimpleJson.DeserializeObject<AppRestResponse>(response.Content);

并添加以下两行:

IRestResponse<AppRestResponse> response = client.Execute<AppRestResponse>(request);
var apiResponse= response.Data;

它按预期工作。