=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 表单路由
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);
}
void RegisterRouters(RouteCollection routes)
{
// MapPageRoute("自订路由名称", "替换後的网址区块", "原本实际执行的网页位置")
// {shortUrl} 为短网址名称,可以视为之後要用来抓取的参数
routes.MapPageRoute("shortUrlRoute", "ShowList/{shortUrl}", "~/Tayanahtml/dealers.aspx");
//可以建立多个规则
}
//取得网址传值的 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 正常进行读取内容及修改
//原网址-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") %>" />
👺 不建议使用 Page.ResolveUrl(),会有被攻击的风险。
👀 ResolveUrl 的 XSS 攻击模式参考 : All is XSS that comes to the .NET
👀 VirtualPathUtility.ToAbsolute() 官网介绍 : VirtualPathUtility.ToAbsolute 方法
📢 原本是在试 Accupass 设定活动页面短网址这种活动功能,尝试去理解它是怎麽做到的,测试成功之後就把它用到专案里面,算是专案制作的小彩蛋,在新建 Global.asax 档时,发现预设还有很多种事件可以使用,如果有 Global.asax 相关的应用欢迎留言讨论,对应用十分好奇,因为感觉可以做到很多事,感谢!
<<: 【Day 07】 在 AWS 中打造出 Data Lake 以及 Data Warehouse
既然昨天已经说了 http post 这件事,那今天就来说说 http get 这部份吧! 今天的资...
Dynamic Programmin的经典应用除了斐波那契数之外,还有背包问题、最短路径问题、河内...
Chap.I 理论基础 Part 4:统计 & 机率 Analyze the data th...
今天想要分享的是这一个 Youtube 影片做出来的等待画面, 我只有挑他的其中一个写, 其他可以看...
公司若是出一系列潜在消费者会使用关键字後,放上 Google Ads 後,Google 是怎麽去花费...