本帖最后由 KearneyKang 于 2022-4-7 09:54 编辑
1、首先在Html页面添加onRequest()的方法,给header赋值
- <script type="text/javascript">
- let viewer;
- function loadViewer() {
- viewer = GrapeCity.ActiveReports.JSViewer.create({
- element: '#viewerContainer',
- reportService: {
- url: 'api/reporting',
- onRequest: function (init) {
- init.headers.Authorization = '123456';
- }
- }
- });
- viewer.openReport("JsonUrl.rdlx");
- }
- </script>
复制代码 2、在Startup中添加类 MvcContext
- public class MvcContext
- {
- public static IHttpContextAccessor httpContextAccessor;
- public static HttpContext GetContext()
- {
- HttpContext context = httpContextAccessor.HttpContext;
- return context;
- }
- }
复制代码 3、修改ConfigureServices;修改Configure- public void ConfigureServices(IServiceCollection services)
- {
- services.AddHttpContextAccessor();
- services
- .AddLogging(config =>
- {
- // Disable the default logging configuration
- config.ClearProviders();
- // Enable logging for debug mode only
- if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
- {
- config.AddConsole();
- }
- })
- .AddReporting()
- .AddMvc(option => option.EnableEndpointRouting = false);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IHttpContextAccessor svp, IAntiforgery antiforgery)
- {
- MvcContext.httpContextAccessor = svp;
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- Path = env.ContentRootPath;
- app.UseReporting(settings =>
- {
- settings.UseCompression = true;
- //settings.UseFileStore(new DirectoryInfo(@"D:\Demo\JS-Viewer\WebCore001\WebCore001\Reports"));
- //settings.UseFileStore(new DirectoryInfo(String.Format(@"{0}.\Reports", path)));
- settings.UseCustomStore(GetReport);//使用该方法可以自定义进行属性的设置和调用
- settings.UseCompression = true;
- });
-
- app.UseMvc();
- }
复制代码
4、获取Headers["Authorization"]:MvcContext.GetContext().Request.Headers["Authorization"].ToString()
报表加载的时候要使用 settings.UseCustomStore()的方法才能获取到Header
- public object GetReport(string P)//获取报表名称和报表参数,进行一个对应的报表名称和参数的分割
- {
- string reportName = P;//报表名称;
- PageReport rep = new PageReport();
-
- rep.Load(new FileInfo(@"" + Path +"/"+ "Reports/" + reportName));
- //数据源连接字符串的修改
- string connect = "jsondoc=http://jsonplaceholder.typicode.com/comments/";
- rep.Report.DataSources[0].ConnectionProperties.ConnectString = connect;
- string A = MvcContext.GetContext().Request.Headers["Authorization"].ToString();
- //rep.Report.DataSources[0].ConnectionProperties.ConnectString = connect;
- //数据集查询语句的修改
- return rep.Report;
- }
复制代码
|