前言
解析 Request URL 時會進行一些判斷來擷取字串,早期常見到會利用 Substring
和 IndexOf
以 https://localhost:44339/Home/Test?id=123#topic 為例:
Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1) - 1);
// <https://localhost/Home/Test> 中取得 Home
方法
實際上 Request 有很多 Property 可以更簡便的取得網址的各個片段
列出幾個常用的屬性,方便日後查詢:
Property | Result |
---|---|
Request.ApplicationPath | / |
Request.PhysicalPath | C:\Code\WebApplication1\Home\Test |
System.IO.Path.GetDirectoryName(Request.PhysicalPath) | C:\Code\WebApplication1\Home |
Request.PhysicalApplicationPath | C:\Code\WebApplication1\ |
System.IO.Path.GetFileName(Request.PhysicalPath) | Test |
Request.CurrentExecutionFilePath | /Home/Test |
Request.FilePath | /Home/Test |
Request.Path | /Home/Test |
Request.RawUrl | /Home/Test?id=123 |
Request.Url.AbsolutePath | /Home/Test |
Request.Url.AbsoluteUri | https://localhost:44339/Home/Test?id=123 |
Request.Url.Scheme | https |
Request.Url.Host | localhost |
Request.Url.Port | 44339 |
Request.Url.Authority | localhost:44339 |
Request.Url.LocalPath | /Home/Test |
Request.PathInfo | |
Request.Url.PathAndQuery | /Home/Test?id=123 |
Request.Url.Query | ?id=123 |
Request.Url.Fragment | |
Request.Url.Segments[1].Replace(” / “, “”) | Home |
上列表格參考的程式碼:
DataTable dt = new DataTable();
// <https://localhost:44339/Home/Test?id=123#topic>
// Request.Url.Segments[1].Replace("/", "");
// Request.ApplicationPath
dt.Columns.Add("Property", typeof(string));
dt.Columns.Add("Result", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = "Request.ApplicationPath";
dr[1] = Request.ApplicationPath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.PhysicalPath";
dr[1] = Request.PhysicalPath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "System.IO.Path.GetDirectoryName(Request.PhysicalPath)";
dr[1] = System.IO.Path.GetDirectoryName(Request.PhysicalPath);
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.PhysicalApplicationPath";
dr[1] = Request.PhysicalApplicationPath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "System.IO.Path.GetFileName(Request.PhysicalPath)";
dr[1] = System.IO.Path.GetFileName(Request.PhysicalPath);
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.CurrentExecutionFilePath";
dr[1] = Request.CurrentExecutionFilePath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.FilePath";
dr[1] = Request.FilePath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Path";
dr[1] = Request.Path;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.RawUrl";
dr[1] = Request.RawUrl;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.AbsolutePath";
dr[1] = Request.Url.AbsolutePath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.AbsoluteUri";
dr[1] = Request.Url.AbsoluteUri;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Scheme";
dr[1] = Request.Url.Scheme;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Host";
dr[1] = Request.Url.Host;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Port";
dr[1] = Request.Url.Port;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Authority";
dr[1] = Request.Url.Authority;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.LocalPath";
dr[1] = Request.Url.LocalPath;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.PathInfo";
dr[1] = Request.PathInfo;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.PathAndQuery";
dr[1] = Request.Url.PathAndQuery;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Query";
dr[1] = Request.Url.Query;
dt.Rows.Add(dr);
// 應該無法取得任何資料,通常 Browser 不會送出 "#topic"
dr = dt.NewRow();
dr[0] = "Request.Url.Fragment";
dr[1] = Request.Url.Fragment;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Request.Url.Segments[1].Replace(\\" / \\", \\"\\")";
dr[1] = Request.Url.Segments[1].Replace("/", "");
dt.Rows.Add(dr);
return View(dt);
@using System.Data
@model DataTable
@if (Model.Rows.Count > 0)
{
<table border="1">
<thead>
<tr>
@foreach (DataColumn col in Model.Columns)
{
<th style="padding: 10px;">@col.ColumnName</th>
}
</tr>
</thead>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td style="padding: 10px;">@row[col.ColumnName]</td>
}
</tr>
}
</table>
}
參考: