asp.net mvc restsharp api。

问题描述 投票:0回答:1
   // GET: GetLogApi
    public ActionResult Index(GetItems getItems, int? PageIndex)
    {
        getItems.PageIndex = PageIndex;
        string uri = string.Format("https://log.egeyazilim.net/api/v1/Logger/Get?logType={0}&logAction={1}&applicationType={2}&clientType={3}&companyCode={4}&comment={5}&applicationModule={6}&method={7}&=ipAddress{8}&url={9}&statusCode={10}&isError={11}&requestedOnUtc={12}&responsedOnUtc={13}&isActive={14}&PageIndex={15}&Limit={16}",
          getItems.LogType ?? "",
          getItems.LogAction ?? "",
          getItems.ApplicationType ?? "",
          getItems.ClientType ?? "",
          getItems.CompanyCode ?? "",
          getItems.Comment ?? "",
          getItems.ApplicationModule ?? "",
          getItems.Method ?? "",
          getItems.IpAddress ?? "",
          getItems.Url ?? "",
          getItems.ResponseStatus ?? "",
          getItems.IsError ? "Checked" : "",
          getItems.StartDate ?? "",
          getItems.EndDate ?? "",
          getItems.isActive ? "Checked" : "",
          getItems.PageIndex ?? 1,
          50);
        var restClient = new RestClient(uri);
        var request = new RestRequest(Method.GET);
        request.AddHeader("X-Api-Key", "C5BFF7F0-B4DF-475E-A331-F737424F013C");

        List<LogInfoGet> logInfoGet = new List<LogInfoGet>();
        GenericReturn<List<LogInfoGet>> genericReturn = new GenericReturn<List<LogInfoGet>>();

        request.RequestFormat = DataFormat.Json;
        IRestResponse response = restClient.Execute<LogInfoGet>(request);

        var requestModel = JsonConvert.DeserializeObject<LogInfoGet>(response.Content);

        return View(requestModel);

    }

public class LogInfoGet{

    [JsonProperty("items")]
    public Item[] Items { get; set; }

    [JsonProperty("currentPageIndex")]
    public long CurrentPageIndex { get; set; }

    [JsonProperty("totalItemsCount")]
    public long TotalItemsCount { get; set; }

    [JsonProperty("filteredItemsCount")]
    public long FilteredItemsCount { get; set; }

    [JsonProperty("displayedItemsCount")]
    public long DisplayedItemsCount { get; set; }

    [JsonProperty("totalPages")]
    public long TotalPages { get; set; }

    [JsonProperty("hasPreviousPage")]
    public bool HasPreviousPage { get; set; }

    [JsonProperty("hasNextPage")]
    public bool HasNextPage { get; set; }

    [JsonProperty("nextPage")]
    public Uri NextPage { get; set; }
}

public partial class Item
{
    [JsonProperty("logType")]
    public string LogType { get; set; }

    [JsonProperty("logAction")]
    public string LogAction { get; set; }

    [JsonProperty("applicationType")]
    public string ApplicationType { get; set; }

    [JsonProperty("clientType")]
    public string ClientType { get; set; }

    [JsonProperty("applicationModule")]
    public string ApplicationModule { get; set; }

    [JsonProperty("companyCode")]
    public string CompanyCode { get; set; }

    [JsonProperty("comment")]
    public string Comment { get; set; }

    [JsonProperty("requestedOnUtc")]
    public DateTimeOffset RequestedOnUtc { get; set; }

    [JsonProperty("responsedOnUtc")]
    public DateTimeOffset ResponsedOnUtc { get; set; }

    [JsonProperty("request")]
    public Request Request { get; set; }

    [JsonProperty("response")]
    public Response Response { get; set; }

    [JsonProperty("entityId")]
    public string EntityId { get; set; }

    [JsonProperty("objectId")]
    public string ObjectId { get; set; }

    [JsonProperty("createdOnUtc")]
    public DateTimeOffset CreatedOnUtc { get; set; }

    [JsonProperty("isActive")]
    public bool IsActive { get; set; }

    [JsonProperty("id")]
    public Guid Id { get; set; }
}

public partial class Request
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("isHttps")]
    public bool IsHttps { get; set; }
}

public partial class Response
{
    [JsonProperty("statusCode")]
    public string StatusCode { get; set; }

    [JsonProperty("isError")]
    public bool IsError { get; set; }
}

}

但我想把它作为一个查询字符串发送,我不想从url中的字符串发送,我编辑了这样的代码。

    public ActionResult Index(GetItems getItems, int? PageIndex)
    {
        getItems.PageIndex = PageIndex;
        string uri = string.Format("https://log.egeyazilim.net");
        var restClient = new RestClient(uri);
        var request = new RestRequest("/api/v1/Logger/Get", Method.GET);
        request.AddHeader("X-Api-Key", "C5BFF7F0-B4DF-475E-A331-F737424F013C");
        request.AddParameter("LogType", getItems.LogType ?? "", ParameterType.QueryString);
        request.AddParameter("LogAction", getItems.LogAction ?? "", ParameterType.QueryString);
        request.AddParameter("ApplicationType", getItems.ApplicationType ?? "", ParameterType.QueryString);
        request.AddParameter("ClientType", getItems.ClientType ?? "", ParameterType.QueryString);
        request.AddParameter("CompanyCode", getItems.CompanyCode ?? "", ParameterType.QueryString);
        request.AddParameter("ApplicationModule", getItems.ApplicationModule ?? "", ParameterType.QueryString);
        request.AddParameter("Method", getItems.Method ?? "", ParameterType.QueryString);
        request.AddParameter("IpAddress", getItems.IpAddress ?? "", ParameterType.QueryString);
        request.AddParameter("Url", getItems.Url ?? "", ParameterType.QueryString);
        request.AddParameter("StatusCode", getItems.ResponseStatus ?? "", ParameterType.QueryString);
        request.AddParameter("IsError", getItems.IsError, ParameterType.QueryString);
        request.AddParameter("RequestedOnUtc", getItems.StartDate ?? "", ParameterType.QueryString);
        request.AddParameter("ResponsedOnUtc", getItems.EndDate ?? "", ParameterType.QueryString);
        request.AddParameter("IsActive", getItems.isActive, ParameterType.QueryString);
        request.AddParameter("PageIndex", getItems.PageIndex ?? 1, ParameterType.QueryString);
        request.AddParameter("Limit", getItems.Limit ?? 50, ParameterType.QueryString);

        request.RequestFormat = DataFormat.Json;
        IRestResponse response = restClient.Execute<LogInfoGet>(request);

        var requestModel = JsonConvert.DeserializeObject<LogInfoGet>(response.Content);
        //request.AddJsonBody(requestModel);
        //logInfoGet.AddRange(requestModel.);
        //genericReturn.ReturnObject = new List<LogInfoGet>();
        //genericReturn.ReturnObject.AddRange();
        return View(requestModel);


    }

我可以从第一行代码中无缝地得到Items[]数组,但在第二块代码中,PageIndex等。但在第二行代码中,PageIndex等。值一样,但Items[]变成Null。如果你能帮助我,我会很高兴。

asp.net asp.net-mvc api restsharp
1个回答
0
投票

当你调用类型化的 Execute<T> 方法,你指示RestSharp为你反序列化。所以调用 DeserializeObject 是没有意义的,你使用一个或另一个。

如果你想让RestSharp来做序列化工作,当你使用默认的序列化器(SummpleJson)时,你可能会陷入困境,因为,它很简单,不像Json.NET那样全面。

如果你得到正确的响应(所以 response.Content 看起来是合法的),但你得到的是空数据,最简单的方法是使用另一个序列器。我看到你已经为Newtonsoft.Json准备了响应模型。那为什么不直接使用它呢?它是 妥善记录,只需添加一行代码。

client.UseNewtonsoftJson();

还有一件事--千万不要把实例化的 RestClient 屡屡。使其成为单体。

© www.soinside.com 2019 - 2024. All rights reserved.