【问题标题】:How to request report from c# console application using Telerik REST API如何使用 Telerik REST API 从 c# 控制台应用程序请求报告
【发布时间】:2017-11-08 07:13:38
【问题描述】:

我有 Telerik REST API,在客户端我使用html5 report-viewer。报告在 html 的报告查看器中成功生成。现在我想通过 c# 控制台应用程序从相同的 API 请求报告。我有搜索但没有任何解决方案。请建议我如何使用 C# 控制台应用程序请求报告。

html5 report-viewer Library

注意:我是 Telerik 报告的初学者。

更新 1:

我已设法使用此 API 文档向服务器发送请求。 Telerik Document for Getting Report

在服务器端我写了 CustomReportResolver 。但现在它现在将InstanceId 发送到控制台客户端。

CustomReportResolver

public class CustomReportResolver : IReportResolver
    {
        public ReportSource Resolve(string reportJsonString)
        {
            var reportDto = JsonConvert.DeserializeObject<ReportDTO>(reportJsonString);

            var connectionStringHandler = new CustomConnectionStringManager(reportDto.CompanyId);


            var reportsPath = HttpContext.Current.Server.MapPath($"~/Reports/{reportDto.ReportPath}");


            var sourceReportSource = new UriReportSource { Uri = reportsPath  + reportDto.ReportName };

         //   sourceReportSource.Parameters.Add(new Telerik.Reporting.Parameter("companyId", reportDto.CompanyId));
            var reportSource = connectionStringHandler.UpdateReportSource(sourceReportSource);
            return reportSource;
        }
    }

注意如果我使用默认的 ReportResolver 自托管 Telerik 服务将 pdf 报告成功发送到控制台,但如果我使用 CustomReportResolver 它不会生成 instanceId

可能是什么问题?

【问题讨论】:

    标签: c# console-application telerik-reporting


    【解决方案1】:

    在浪费了很多时间之后,找到了如何从 Telerik 自托管 Web 服务获取PDF(或任何其他报告格式)文档的解决方案。以下是要遵循的一般步骤。

    1. 获取客户端 ID
    2. 获取实例 ID
    3. 获取文档 ID
    4. 下载文档

    下面是一步一步的代码:

    static HttpClient client = new HttpClient();
            static string reportServerAddress = "http://localhost:60031/";
            static string serverREStAPI = reportServerAddress + "api/";
    
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("Demo started");
    
                    RunAsync().Wait();
    
                    Console.WriteLine("Demo ended");
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.ReadKey();
                }
            }
    
            static async Task RunAsync()
            {
               // readFile();
               // return;
                client.BaseAddress = new Uri(serverREStAPI);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
    
    
    
                /*
                 * Steps To get PDF documents from Telerik Self hosted Web Service
                 * Step 1) Get Client Id,
                 * Step 2) Get Instance Id
                 * Step 3) Get Document Id
                 * Step 4) Download Document
                 * 
                 * */
    
                var clientId = await GetClientIdAsync(serverREStAPI + "reports/clients", "clientId");
    
                var instanceId =
                    await GetInstanceAsync(serverREStAPI + $"reports/clients/{clientId}/instances", "instanceId");
                var documentId =
                    await GetDocumentAsync(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents",
                        "documentId");
    
                  await DownloadPDF(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents/{documentId}", true);
    
    
            }
    
    
    
    
    
            static async Task<string> GetClientIdAsync(string path, string paramName)
            {
    
                HttpResponseMessage response = await client.PostAsJsonAsync(path, "");
                response.EnsureSuccessStatusCode();
    
                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                return result[paramName];
            }
    
            static async Task<string> GetInstanceAsync(string path, string paramName)
            {
    
                /*
                 * For Default resolver in Service
                 * */
                var paramterValues = new {CompanyId = 1};
                // var data = new { report = "{ \"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}", parameterValues = "{\"CompanyId\": \"1\"}" };    
                var data = new
                {
                    report = "{\"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}",
                    parameterValues = paramterValues
                };
    
    
    
    
    
                HttpResponseMessage response = await client.PostAsJsonAsync(path, data);
                response.EnsureSuccessStatusCode();
    
                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                return result[paramName];
            }
    
            static async Task<string> GetDocumentAsync(string path, string paramName)
            {
    
    
    
                var data = new {format = "PDF"}; //PDF,XLS,MHTML
                HttpResponseMessage response = await client.PostAsJsonAsync(path, data);
                response.EnsureSuccessStatusCode();
    
                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                return result[paramName];
            }
    
    
    
            static async Task DownloadPDF(string path, bool asAttachment)
            {
                var queryString = "";
    
                // if (asAttachment)
                //  {
                //  queryString += "?content-disposition=attachment";
                //  }
    
    
    
                var filePathAndName = @"D:\testing\tet.html";
                //   File.Create(filePathAndName);
    
                //  string filePath = System.IO.Path.Combine(folderName, fileName);
    
                //System.IO.File.WriteAllText(filePathAndName, result);
    
    
                using (System.Net.WebClient myWebClient = new System.Net.WebClient())
                {
                    await myWebClient.DownloadFileTaskAsync(new Uri(path + queryString), filePathAndName);
                }
    
    
    
                System.Diagnostics.Process.Start(filePathAndName);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2022-10-21
      • 1970-01-01
      相关资源
      最近更新 更多