-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathAutoRoute.cs
More file actions
81 lines (68 loc) · 2.43 KB
/
Copy pathAutoRoute.cs
File metadata and controls
81 lines (68 loc) · 2.43 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Routing;
// Author: adriancs
// Github: https://github.com/adriancs2/Auto-Route
namespace adriancs
{
public class AutoRoute
{
/// <summary>
/// Route all ASPX pages, including sub-folders.
/// </summary>
/// <param name="folder">The web path to the folder. For example: "~/pages" or simply start from the root folder "~/"</param>
public static void Route(string folder)
{
Route(folder, true);
}
/// <summary>
/// Route all ASPX pages
/// </summary>
/// <param name="folder">The web path to the folder. For example: "~/pages" or simply start from the root folder "~/"</param>
/// <param name="includeSubFolder">Specifies whether to include pages in sub-folders or not</param>
public static void Route(string folder, bool includeSubFolder)
{
string rootFolder = HttpContext.Current.Server.MapPath("~/");
if (folder.StartsWith("~/"))
{ }
else if (folder.StartsWith("/"))
{
folder = "~" + folder;
}
else
{
folder = "~/" + folder;
}
folder = HttpContext.Current.Server.MapPath(folder);
MapPageRoute(folder, rootFolder, includeSubFolder);
}
static void MapPageRoute(string folder, string rootFolder, bool includeSubFolder)
{
if (includeSubFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder, includeSubFolder);
}
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
if (file.EndsWith(".aspx"))
{
string webPath = file.Replace(rootFolder, "~/").Replace("\\", "/");
var filename = Path.GetFileNameWithoutExtension(file);
if (filename.ToLower() == "default")
{
continue;
}
RouteTable.Routes.MapPageRoute(filename, filename, webPath);
}
}
}
}
}