找回密码
 立即注册

QQ登录

只需一步,快速开始

wengMQ 悬赏达人认证

银牌会员

43

主题

737

帖子

2098

积分

银牌会员

积分
2098

悬赏达人圣诞拼拼乐微信认证勋章

wengMQ 悬赏达人认证
银牌会员   /  发表于:2022-4-6 22:29  /   查看:2428  /  回复:1
本帖最后由 KearneyKang 于 2022-4-7 09:54 编辑

1、首先在Html页面添加onRequest()的方法,给header赋值
  1.   <script type="text/javascript">
  2.     let viewer;
  3.     function loadViewer() {
  4.       viewer = GrapeCity.ActiveReports.JSViewer.create({
  5.           element: '#viewerContainer',
  6.           reportService: {
  7.               url: 'api/reporting',
  8.               onRequest: function (init) {
  9.                   init.headers.Authorization = '123456';
  10.               }
  11.           }
  12.       });
  13.         viewer.openReport("JsonUrl.rdlx");
  14.     }
  15.   </script>
复制代码
2、在Startup中添加类 MvcContext
  1. public class MvcContext
  2.     {
  3.         public static IHttpContextAccessor httpContextAccessor;
  4.         public static HttpContext GetContext()
  5.         {
  6.             HttpContext context = httpContextAccessor.HttpContext;
  7.             return context;
  8.         }

  9.     }
复制代码
3、修改ConfigureServices;修改Configure
  1. public void ConfigureServices(IServiceCollection services)
  2.         {
  3.             services.AddHttpContextAccessor();
  4.             services
  5.                 .AddLogging(config =>
  6.                 {
  7.                     // Disable the default logging configuration
  8.                     config.ClearProviders();

  9.                     // Enable logging for debug mode only
  10.                     if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
  11.                     {
  12.                         config.AddConsole();
  13.                     }
  14.                 })
  15.                 .AddReporting()
  16.                 .AddMvc(option => option.EnableEndpointRouting = false);
  17.         }

  18.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  19.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IHttpContextAccessor  svp, IAntiforgery antiforgery)
  20.         {
  21.             MvcContext.httpContextAccessor = svp;
  22.             if (env.IsDevelopment())
  23.             {
  24.                 app.UseDeveloperExceptionPage();
  25.             }
  26.             Path = env.ContentRootPath;           
  27.             app.UseReporting(settings =>
  28.             {
  29.                 settings.UseCompression = true;

  30.                 //settings.UseFileStore(new DirectoryInfo(@"D:\Demo\JS-Viewer\WebCore001\WebCore001\Reports"));      
  31.                 //settings.UseFileStore(new DirectoryInfo(String.Format(@"{0}.\Reports", path)));
  32.                 settings.UseCustomStore(GetReport);//使用该方法可以自定义进行属性的设置和调用         
  33.                 settings.UseCompression = true;

  34.             });
  35.       
  36.           app.UseMvc();
  37.         }
复制代码


4、获取Headers["Authorization"]:MvcContext.GetContext().Request.Headers["Authorization"].ToString()
报表加载的时候要使用  settings.UseCustomStore()的方法才能获取到Header
  1. public object GetReport(string P)//获取报表名称和报表参数,进行一个对应的报表名称和参数的分割
  2.         {
  3.             string reportName = P;//报表名称;                          
  4.             PageReport rep = new PageReport();
  5.            
  6.             rep.Load(new FileInfo(@"" + Path +"/"+ "Reports/" + reportName));
  7.             //数据源连接字符串的修改
  8.             string connect = "jsondoc=http://jsonplaceholder.typicode.com/comments/";
  9.             rep.Report.DataSources[0].ConnectionProperties.ConnectString = connect;
  10.             string A = MvcContext.GetContext().Request.Headers["Authorization"].ToString();
  11.             //rep.Report.DataSources[0].ConnectionProperties.ConnectString = connect;           
  12.             //数据集查询语句的修改      
  13.             return rep.Report;
  14.         }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

评分

参与人数 2金币 +2000 收起 理由
KearneyKang + 1000 已经验证非常给力
Bella.Yuan + 1000 很给力!

查看全部评分

1 个回复

倒序浏览
Bella.YuanWyn认证
超级版主   /  发表于:2022-4-7 08:54:56
沙发
感谢老铁
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部