目录
介绍
有几个因素会影响URL在ASP.NET中的路径。
为了组织页面,它们通常与文件夹分组。
这是页面的典型URL之一。
/pages/admin/setup/user/v2/EditUser.aspx
页面“EditUser.aspx”位于“v2”文件夹中,该文件夹位于“user”文件夹中,该文件夹位于“setup”文件夹中,该文件夹位于另一个文件夹“admin”中,该文件夹...再次落入另一个名为“pages”的文件夹。
随着项目越来越大,越来越多的根文件夹将分散在项目中。
由于文件夹路径与文件的URL路径严格绑定,因此会使URL变得很长。
ASP.NET提供了一个有用的函数调用“路由”,可以解决长URL问题。
以下是它的实现方式。
在项目的根目录中,添加一个“全局应用程序类”文件,即“Global.asax”。
打开文件“Global.asax”。
通过添加using语句导入路由库:
using System.Web.Routing;
在Application_Start方法:
protected void Application_Start(object sender, EventArgs e)
{
}
键入路由代码:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("EditUser", "EditUser",
"~/admin/setup/user/v2/EditUser.aspx");
}
这将创建以下URL:
/admin/setup/user/v2/EditUser.aspx
缩短为:
/EditUser
到目前为止看起来不错....
如果。。。有很多(是的...一大堆)的页面?
好吧,最后你要把它们一个接一个地路由......例如:
RouteTable.Routes.MapPageRoute("UserEdit", "UserEdit",
"~/admin/setup/user/v2/UserEdit.aspx");
RouteTable.Routes.MapPageRoute("UserList", "UserList",
"~/admin/setup/user/v2/UserList.aspx");
RouteTable.Routes.MapPageRoute("UserCategory", "UserCategory",
"~/admin/setup/user/v2/UserCategory.aspx");
RouteTable.Routes.MapPageRoute("UserCategoryEdit", "UserCategoryEdit",
"~/admin/setup/user/v2/UserCategoryEdit.aspx");
RouteTable.Routes.MapPageRoute("InvoicePreview", "InvoicePreview",
"~/invoice/user/v3/InvoicePreview.aspx");
RouteTable.Routes.MapPageRoute("InvoiceSarch", "InvoiceSarch",
"~/invoice/user/v3/InvoiceSarch.aspx");
// ...
// continue for yet another lots of... lots of pages...
// ....
是的,你明白了。那将是很多台词。大量的路由代码行将使维护工作变得痛苦。错误可能很难被监视。
但是,怎么样...在一行中自动路由所有页面?
这是我们要做的
仅使用一个根文件夹来组织所有页面。
例如,而不是像这样有多个根文件夹:
/settings
/user
/activity
/invoice
/member
/inventory
....
将它们完全放在一个文件夹中,如下所示:
/pages/settings
/pages/user
/pages/activity
/pages/invoice
/pages/member
/pages/inventory
然后,返回到“Global.asax”文件以编写路由命令。
在该Application_Start方法中,键入以下路由命令:
protected void Application_Start(object sender, EventArgs e)
{
RouteFolder("~/pages");
}
以下是方法的内容:RouteFolder
public static void RouteFolder(string folder)
{
}
获取根目录路径:
public static void RouteFolder(string folder)
{
string rootFolder = HttpContext.Current.Server.MapPath("~/");
}
用“folder~/”双重确认参数前缀:
public static void RouteFolder(string folder)
{
string rootFolder = HttpContext.Current.Server.MapPath("~/");
if (folder.StartsWith("~/"))
{ }
else if (folder.StartsWith("/"))
{
folder = "~" + folder;
}
else
{
folder = "~/" + folder;
}
}
以另一种单独的方法开始路由文件夹:
public static void RouteFolder(string folder)
{
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);
}
MapPageRoute方法。首先获取子文件夹:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
}
执行递归循环以路由子文件夹:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
}
获取文件夹中的所有文件:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
}
}
遍历所有文件,如果页面不是ASP.NET页面,请跳过它。
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
if (!file.EndsWith(".aspx"))
continue;
}
}
获取文件的网址路径:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
if (!file.EndsWith(".aspx"))
continue;
string webPath = file.Replace(rootFolder, "~/").Replace("\\", "/");
}
}
示例:以下文件路径:
file = "D:\wwwroot\website1\pages\user\UserProfile.aspx"
将成为:
webPath = "~/pages/user/UserProfile.aspx"
接下来,获取文件名:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
// not a page, skip action
if (!file.EndsWith(".aspx"))
continue;
string webPath = file.Replace(rootFolder, "~/").Replace("\\", "/");
var filename = Path.GetFileNameWithoutExtension(file);
}
}
输出:
filename = "UserProfile"
接下来,如果它是一个default页面,请跳过它。始终为default页面放置空内容。使用default页面作为登录页面,将用户重定向到对应/适当/真实页面。
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
// not a page, skip action
if (!file.EndsWith(".aspx"))
continue;
string webPath = file.Replace(rootFolder, "~/").Replace("\\", "/");
var filename = Path.GetFileNameWithoutExtension(file);
if (filename.ToLower() == "default")
{
continue;
}
}
}
最后,执行路由:
static void MapPageRoute(string folder, string rootFolder)
{
// obtain sub-folders
string[] folders = Directory.GetDirectories(folder);
foreach (var subFolder in folders)
{
MapPageRoute(subFolder, rootFolder);
}
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
// not a page, skip action
if (!file.EndsWith(".aspx"))
continue;
string webPath = file.Replace(rootFolder, "~/").Replace("\\", "/");
var filename = Path.GetFileNameWithoutExtension(file);
if (filename.ToLower() == "default")
{
continue;
}
RouteTable.Routes.MapPageRoute(filename, filename, webPath);
}
}
所以现在,与其这样做:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("UserEdit", "UserEdit",
"~/pages/admin/setup/user/v2/UserEdit.aspx");
RouteTable.Routes.MapPageRoute("UserList", "UserList",
"~/pages/admin/setup/user/v2/UserList.aspx");
RouteTable.Routes.MapPageRoute("UserCategory", "UserCategory",
"~/pages/admin/setup/user/v2/UserCategory.aspx");
RouteTable.Routes.MapPageRoute("UserCategoryEdit", "UserCategoryEdit",
"~/pages/admin/setup/user/v2/UserCategoryEdit.aspx");
RouteTable.Routes.MapPageRoute("InvoicePreview", "InvoicePreview",
"~/pages/invoice/user/v3/InvoicePreview.aspx");
RouteTable.Routes.MapPageRoute("InvoiceSarch", "InvoiceSarch",
"~/pages/invoice/user/v3/InvoiceSarch.aspx");
// ...
// continue for yet another lots of... lots of pages...
// ....
}
您只需要一行:
protected void Application_Start(object sender, EventArgs e)
{
RouteFolder("~/pages");
}
瞧!它完成了,就像魔术一样。所有页面的路由都没有监督(对路由命令进行1个编码)。
所有这些页面都具有令人困惑,复杂的URL路径(由于组织页面):
/pages/admin/setup/user/v2/EditUser.aspx
/pages/admin/system/settings/AppConfig.aspx
/pages/user/ViewUserProfile.aspx
/pages/activity/ActivityEventList.aspx
/pages/invoice/v3/PrintInvoice.aspx
/pages/member/group/EditMemberList.aspx
/pages/departments/inventory/setup/InventoryCategory.aspx
现在将全部缩短为:
/EditUser
/AppConfig
/ViewUserProfile
/ActivityEventList
/PrintInvoice
/EditMemberList
/InventoryCategory
您只需要注意不要在不同的文件夹中重复(重用)相同的文件名。
例如(不要这样做):
/pages/member/Search.aspx
/pages/team/Search.aspx
相反,将节名称放在文件名中,如下所示:
/pages/member/SearchMember.aspx
/pages/team/SearchTeam.aspx
页面将按如下方式路由到:
/SearchMember
/SearchTeam
好的,这就是本文的全部内容。
https://www.codeproject.com/Articles/5346909/Automatic-Route-All-Pages-in-ASP-NET-WebForms