-
Notifications
You must be signed in to change notification settings - Fork 11
/
Global.asax.cs
204 lines (192 loc) · 7.72 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Hosting;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using ReportsWebFormsSamples.Models;
using System.Text;
using Bold.Licensing;
using System.IO;
using BoldReports.Base.Logger;
using BoldReports.Web;
using Newtonsoft.Json;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace ReportsWebFormsSamples
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
log4net.GlobalContext.Properties["LogPath"] = this.GetAppDataFolderPath();
BoldReports.Base.Logger.LogExtension.RegisterLog4NetConfig();
// Register Bold Reports license
string License = File.ReadAllText(Server.MapPath("BoldLicense.txt"), Encoding.UTF8);
BoldLicenseProvider.RegisterLicense(License, bool.Parse(System.Configuration.ConfigurationManager.AppSettings["IsOfflineLicense"]));
ReportConfig.DefaultSettings = new ReportSettings()
{
MapSetting = this.GetMapSettings()
}.RegisterExtensions(this.GetDataExtension());
// Code that runs on application startup
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
private List<string> GetDataExtension()
{
var extensions = !string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["ExtAssemblies"]) ? System.Configuration.ConfigurationManager.AppSettings["ExtAssemblies"] : string.Empty;
try
{
var ExtNames = new List<string>(extensions.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries));
List<string> ExtCollections = new List<string>();
ExtNames.ForEach(Extension => ExtCollections.Add(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin", Extension + ".dll")));
return ExtCollections;
}
catch (Exception ex)
{
LogExtension.LogError("Failed to Load Data Extension", ex, MethodBase.GetCurrentMethod());
}
return null;
}
ReportSample getSampleData(string reportBasePath, string reportRouterPath, ReportSample sampleData)
{
dynamic samples = SampleData.getSampleData().samples;
foreach (dynamic sample in samples)
{
if (sample.routerPath == reportRouterPath && sample.basePath == reportBasePath)
{
sampleData.isReportSample = true;
sampleData.routerPath = reportRouterPath;
sampleData.basePath = reportBasePath;
}
}
return sampleData;
}
void Application_BeginRequest(Object sender, EventArgs e)
{
ReportSample sampleData = new ReportSample(); ;
int pathLen;
string path = HttpContext.Current.Request.Path;
string basePath = HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : (HttpContext.Current.Request.ApplicationPath + "/");
bool isinitialRouting = basePath == path || (basePath == path + "/");
bool isRDLCReportDesigner = path.Contains("/ReportDesigner/RDLC") && !path.Contains("/api/");
bool isReportDesigner = path.Contains("/ReportDesigner") && !path.Contains("/api/");
string[] urlPaths = new Regex(basePath).Replace(path, "", 1).Split('/');
pathLen = urlPaths.Length;
if (pathLen > 0 && !isinitialRouting && !isReportDesigner && !path.Contains("/api/"))
{
string reportBasePath = urlPaths[0];
string reportRouterPath;
if (path.Contains("/Preview"))
{
reportRouterPath = urlPaths[1] != "Preview" ? urlPaths[1] : "";
}
else
{
reportRouterPath = pathLen > 1 ? urlPaths[1] : "";
}
sampleData = getSampleData(reportBasePath, reportRouterPath, sampleData);
}
HttpContext.Current.Items["isPreview"] = path.Contains("/Preview") || path.Contains("/ReportDesigner");
bool sourceTab = HttpContext.Current.Request.QueryString.ToString().Contains("sourceTab=true");
if (sourceTab)
{
string res = System.IO.File.ReadAllText(HttpContext.Current.Request.PhysicalPath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "text/plain");
Response.Write(res);
Response.Flush();
Response.End();
}
else if (isinitialRouting)
{
HttpContext.Current.Response.RedirectPermanent(basePath + "ReportViewer/ProductLineSales");
}
else if (isRDLCReportDesigner)
{
HttpContext.Current.Items["designerType"] = "RDLC";
Context.RewritePath(path.Replace(path, "~/Views/RDLC/Index.aspx"));
}
else if (isReportDesigner)
{
HttpContext.Current.Items["designerType"] = "RDL";
Context.RewritePath(path.Replace(path, "~/Views/ReportDesigner/Index.aspx"));
}
else if (sampleData.isReportSample)
{
string reportRouterPath = sampleData.routerPath == "" ? sampleData.basePath : sampleData.routerPath;
HttpContext.Current.Items["sampleName"] = reportRouterPath;
Context.RewritePath(path.Replace(path, "~/Views/" + reportRouterPath + "/Index.aspx"));
}
}
public string GetAppDataFolderPath()
{
try
{
return System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory);
}
catch
{
return null;
}
}
private BoldReports.Web.MapSetting GetMapSettings()
{
try
{
string basePath = HttpContext.Current.Server.MapPath("~/Scripts");
return new MapSetting()
{
ShapePath = basePath + "\\ShapeData\\",
MapShapes = JsonConvert.DeserializeObject<List<MapShape>>(System.IO.File.ReadAllText(basePath + "\\ShapeData\\mapshapes.txt"))
};
}
catch (Exception ex)
{
LogExtension.LogError("Failed to Load Map Settings", ex, MethodBase.GetCurrentMethod());
}
return null;
}
}
public class CSRFHandler
{
public static void Validate(Page page, HiddenField forgeryToken)
{
Guid antiforgeryToken = Guid.NewGuid();
page.Session["AntiforgeryToken"] = antiforgeryToken;
forgeryToken.Value = antiforgeryToken.ToString();
}
}
public class ReportSample
{
public bool isReportSample
{
get;
set;
}
public string basePath
{
get;
set;
}
public string routerPath
{
get;
set;
}
public ReportSample()
{
this.isReportSample = false;
this.basePath = "";
this.routerPath = "";
}
}
}