-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.ascx.cs
More file actions
133 lines (116 loc) · 4.22 KB
/
Copy pathView.ascx.cs
File metadata and controls
133 lines (116 loc) · 4.22 KB
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
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;
using System;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace DnnFree.Modules.SPA.Angular
{
public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase
{
public string ErrorMessage { get; set; } = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
try
{
RegisterAngularAssets();
InjectTokenToPage();
}
catch (Exception ex)
{
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}
private void InjectTokenToPage()
{
if (Page.Header == null) return;
string token = GetAntiForgeryToken();
var metaTag = new HtmlMeta
{
Name = "RequestVerificationToken",
Content = token
};
Page.Header.Controls.Add(metaTag);
}
private string GetAntiForgeryToken()
{
try
{
// برای DNN 9.4+
var tokenField = typeof(DotNetNuke.Web.Common.SharedConstants)
.GetField("RequestVerificationToken");
if (tokenField != null)
{
return tokenField.GetValue(null)?.ToString() ?? ModuleId.ToString();
}
}
catch { }
return ModuleId.ToString();
}
private void RegisterAngularAssets()
{
string moduleVirtualPath = AppRelativeTemplateSourceDirectory;
string distVirtualPath = $"{moduleVirtualPath.TrimEnd('/')}/dist";
string distPhysicalPath = Server.MapPath(distVirtualPath);
if (!Directory.Exists(distPhysicalPath))
{
ErrorMessage = $"<div class='dnnFormMessage dnnFormError'><strong>Error:</strong> Angular dist folder not found at: {distPhysicalPath}</div>";
DotNetNuke.Services.Exceptions.Exceptions.LogException(
new DirectoryNotFoundException($"Angular dist folder not found: {distPhysicalPath}")
);
return;
}
LoadJsFiles(distPhysicalPath, distVirtualPath);
LoadCssFiles(distPhysicalPath, distVirtualPath);
}
private void LoadJsFiles(string physicalPath, string virtualPath)
{
var priorityOrder = new[] { "runtime", "polyfills", "main" };
int basePriority = (int)FileOrder.Js.DefaultPriority;
var allJsFiles = Directory
.GetFiles(physicalPath, "*.js")
.Select(f => Path.GetFileName(f))
.ToList();
foreach (var prefix in priorityOrder)
{
var match = allJsFiles.FirstOrDefault(f =>
f.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
if (match != null)
{
ClientResourceManager.RegisterScript(
Page,
$"{virtualPath}/{match}",
basePriority++
);
allJsFiles.Remove(match);
}
}
foreach (var chunkFile in allJsFiles.OrderBy(f => f))
{
ClientResourceManager.RegisterScript(
Page,
$"{virtualPath}/{chunkFile}",
basePriority++
);
}
}
private void LoadCssFiles(string physicalPath, string virtualPath)
{
var cssFiles = Directory
.GetFiles(physicalPath, "*.css")
.Select(f => Path.GetFileName(f))
.OrderBy(f => f);
int priority = (int)FileOrder.Css.DefaultPriority;
foreach (var cssFile in cssFiles)
{
ClientResourceManager.RegisterStyleSheet(
Page,
$"{virtualPath}/{cssFile}",
priority++
);
}
}
}
}