Day 7 - Using Global.asax File for Short URL Routing with ASP.NET Web Forms C# 使用全域应用程序类别产生短网址路由功能

=x= 🌵 网址显示方式管理。


短网址功能介绍 :

📌 如果仔细观察网址,可以发现平常浏览的网页并不会出现副档名,但是用 Visual Studio 跑出来的网页都会有副档名,而且网址就是资料夹的名称跟档名的组合,今天要介绍的就是使用全域应用程序类别 Global.asax 来进行网址的管理,如何设定不显示副档名,以及用来自订短网址的制作方式。

👀 短网址案例 : Accupass 设定活动页面短网址

👀 Global.asax 介绍 : Global.asax 文件是什麽

👀 应用程序生命周期概述 : ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0

👀 Global.asax 官网说明 : Global.asax Syntax

👀 Route 路由官网说明 : 终极 ASP.NET: 使用 ASP.NET Web 表单路由



短网址功能实作 :

1. 使用 Nuget 安装 Microsoft.AspNet.FriendlyUrls 套件 (设定不显示副档名用)

https://ithelp.ithome.com.tw/upload/images/20210919/20139487lZ6eccj3mJ.jpg


2. 专案右键新增全域应用程序类别 Global.asax

https://ithelp.ithome.com.tw/upload/images/20210919/20139487T2pOM2af3f.jpg

  • 🌵 由於一个专案只会有一个 Global.asax,可以发现如果已新增过,再次右键时就无法看到这个选项了。

3. 在 Global.asax 的 Application_Start 事件内加入以下程序码

protected void Application_Start(object sender, EventArgs e)
{
    // 设定不显示副档名 (如果只想隐藏副档名做到此区块就好)
    var routes = RouteTable.Routes;
    var settings = new FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);

    // 执行短网址路由方法
    RegisterRouters(RouteTable.Routes);
}


4. 在 Global.asax 中建立 RegisterRouters 方法

void RegisterRouters(RouteCollection routes)
{
    // MapPageRoute("自订路由名称", "替换後的网址区块", "原本实际执行的网页位置")
        // {shortUrl} 为短网址名称,可以视为之後要用来抓取的参数
    routes.MapPageRoute("shortUrlRoute", "ShowList/{shortUrl}", "~/Tayanahtml/dealers.aspx");
    //可以建立多个规则
}


5. 在想要使用短网址的页面後置程序码建立判断逻辑 (示范页面 : DEALERS Page)

//取得网址传值的 id 内容
string urlIDStr = Request.QueryString["id"];

SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TayanaYachtConnectionString"].ConnectionString);
//如果是用短网址连入则用短网址 shortUrl 参数内容的国家名称来判断 ID
if (Page.RouteData.Values.Count > 0) {
    //取得短网址参数内容的国家名称
    string urlCountryStr = Page.RouteData.Values["shortUrl"].ToString();
    string sqlID = "SELECT id FROM CountrySort WHERE countrySort = @urlCountryStr";
    SqlCommand commandID = new SqlCommand(sqlID, connection);
    commandID.Parameters.AddWithValue("@urlCountryStr", urlCountryStr);
    connection.Open();
    SqlDataReader readerID = commandID.ExecuteReader();
    if (readerID.Read()) {
        urlIDStr = readerID["id"].ToString();
    }
    connection.Close();
}

//如无网址传值则设为第一笔国家名称 id
if (string.IsNullOrEmpty(urlIDStr)) {
    string sql = "SELECT TOP 1 id FROM CountrySort";
    SqlCommand command = new SqlCommand(sql, connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    if (reader.Read()) {
        urlIDStr = reader["id"].ToString();
    }
    connection.Close();
}

//将 id 存入 Session 使用
Session["id"] = urlIDStr;

//下方依取得 ID 正常进行读取内容及修改


6. 用短网址连入後会发现图片掉图,可依以下方式修改

//原网址-https://localhost:44305/Tayanahtml/dealers?id=1
//短网址-https://localhost:44305/ShowList/USA
//上面两个网址会得到相同网页内容

//图片位置修改方式1 : (用後置程序码 .aspx.cs 送出 Html 也适用)
<img src="../Tayanahtml/images/newbanner.jpg" />

//图片位置修改方式2 : (仅 .aspx 页面适用)
<img src="<%= VirtualPathUtility.ToAbsolute("~/Tayanahtml/images/newbanner.jpg") %>" />

7. 确认不同网址切换都能成功显示网页,完成~



本日总结 :

📢 原本是在试 Accupass 设定活动页面短网址这种活动功能,尝试去理解它是怎麽做到的,测试成功之後就把它用到专案里面,算是专案制作的小彩蛋,在新建 Global.asax 档时,发现预设还有很多种事件可以使用,如果有 Global.asax 相关的应用欢迎留言讨论,对应用十分好奇,因为感觉可以做到很多事,感谢!

  • 明日将介绍制作 Dealer Manager - Content Page 後台的相关细节。

<<:  【Day 07】 在 AWS 中打造出 Data Lake 以及 Data Warehouse

>>:  [Day 13] SRE - 悟

Angular 如何取得 API 资料

既然昨天已经说了 http post 这件事,那今天就来说说 http get 这部份吧! 今天的资...

Day26:Dynamic Programming(DP) - 动态规划(下)

Dynamic Programmin的经典应用除了斐波那契数之外,还有背包问题、最短路径问题、河内...

Python 演算法 Day 6 - 理论基础 统计 & 机率

Chap.I 理论基础 Part 4:统计 & 机率 Analyze the data th...

Day 27 | 等待的时间不无聊 - loader

今天想要分享的是这一个 Youtube 影片做出来的等待画面, 我只有挑他的其中一个写, 其他可以看...

Day 06 关键字的出价策略

公司若是出一系列潜在消费者会使用关键字後,放上 Google Ads 後,Google 是怎麽去花费...