自动路由ASP.NET Web窗体中的所有页面

文章介绍了如何通过ASP.NET的路由功能解决长URL问题,特别是当项目中包含大量页面时。通过在Global.asax文件中编写路由代码,可以自动为所有页面创建简洁的URL。建议将所有页面放在一个根文件夹下,然后使用递归方法遍历文件夹结构,为每个.aspx页面创建路由,从而避免手动为每个页面配置路由的繁琐工作。
摘要由CSDN通过智能技术生成

目录

介绍

这是我们要做的


介绍

有几个因素会影响URLASP.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值