ASP.NET 如何取得 Request URL 的各个部分

[ 3206 查看 / 0 回复 ]

我们在开发Web应用程序,时常需要去解析网址(Request.Url)的每个片段,进行一些判断。例如说 "http://localhost:1897/News/Press/Content.aspx/123?id=1#toc",我们想要取得网址里第一层目录的名字(News)用以判断不同的页面标题(Page Title)。



我看很多人都用字符串的 IndexOf 方法与 Substring 方法:



Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/",1)-1)



这实在太埋没 .NET 的强大设计了,事实上在 Request 对象就已经提供很多方便的属性(Property)可供取得网址的片段。


底下这张表就是各种跟 Browser Request 的网址相关的属性与用法:


URL http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath /
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx
Request.Path /News/Press/Content.aspx/123
Request.RawUrl /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath /News/Press/Content.aspx/123
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1897
Request.Url.Authority localhost:1897
Request.Url.LocalPath /News/Press/Content.aspx/123
Request.PathInfo /123
Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1
Request.Url.Query ?id=1
Request.Url.Fragment
Request.Url.Segments /
News/
Press/
Content.aspx/
123

所以当你看了这张表之后,你还会想用 Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/",1)-1) 这种写法吗?

用这样写 Request.Url.Segments[1].Replace("/", "") 不是又短又直觉吗? ^_^



以下是产生以上表格的程序代码:



  1. protected void Page_Load(object sender, EventArgs e)
  2.     {

  3.         StringBuilder sb = newStringBuilder();


  4.         sb.Append("");


  5.       sb.Append("");
  6.         sb.Append("网址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc");
  7.         sb.Append("");


  8.         // Request.ApplicationPath
  9.       sb.Append("");
  10.       sb.Append("Request.ApplicationPath");
  11.       sb.Append("");
  12.         sb.Append("" +Request.ApplicationPath + "");
  13.         sb.Append("");


  14.         // Request.PhysicalPath
  15.       sb.Append("");
  16.         sb.Append("Request.PhysicalPath");
  17.       sb.Append("");
  18.         sb.Append("" +Request.PhysicalPath + "");
  19.       sb.Append("");


  20.         // System.IO.Path.GetDirectoryName(Request.PhysicalPath)
  21.       sb.Append("");
  22.         sb.Append("System.IO.Path.GetDirectoryName(Request.PhysicalPath)");
  23.       sb.Append("");
  24.         sb.Append("" +System.IO.Path.GetDirectoryName(Request.PhysicalPath) +"");
  25.       sb.Append("");


  26.         // Request.PhysicalApplicationPath
  27.         sb.Append("");
  28.       sb.Append("Request.PhysicalApplicationPath");
  29.       sb.Append("");
  30.         sb.Append("" +Request.PhysicalApplicationPath + "");
  31.       sb.Append("");


  32.         // System.IO.Path.GetFileName(Request.PhysicalPath)
  33.       sb.Append("");
  34.       sb.Append("System.IO.Path.GetFileName(Request.PhysicalPath)");
  35.         sb.Append("");
  36.         sb.Append("" +System.IO.Path.GetFileName(Request.PhysicalPath) + "");
  37.       sb.Append("");


  38.         // Request.CurrentExecutionFilePath
  39.       sb.Append("");
  40.         sb.Append("Request.CurrentExecutionFilePath");
  41.       sb.Append("");
  42.         sb.Append("" +Request.CurrentExecutionFilePath + "");
  43.       sb.Append("");


  44.         // Request.FilePath
  45.       sb.Append("");
  46.       sb.Append("Request.FilePath");
  47.         sb.Append("");
  48.         sb.Append("" +Request.FilePath + "");
  49.       sb.Append("");


  50.         // Request.Path
  51.         sb.Append("");
  52.         sb.Append("Request.Path");
  53.       sb.Append("");
  54.         sb.Append("" +Request.Path + "");
  55.       sb.Append("");


  56.         // Request.RawUrl
  57.       sb.Append("");
  58.       sb.Append("Request.RawUrl");
  59.         sb.Append("");
  60.         sb.Append("" +Request.RawUrl + "");
  61.         sb.Append("");


  62.         // Request.Url.AbsolutePath
  63.       sb.Append("");
  64.       sb.Append("Request.Url.AbsolutePath");
  65.       sb.Append("");
  66.         sb.Append("" +Request.Url.AbsolutePath + "");
  67.       sb.Append("");


  68.         // Request.Url.AbsoluteUri
  69.       sb.Append("");
  70.       sb.Append("Request.Url.AbsoluteUri");
  71.       sb.Append("");
  72.         sb.Append("" +Request.Url.AbsoluteUri + "");
  73.       sb.Append("");


  74.         // Request.Url.Scheme
  75.       sb.Append("");
  76.       sb.Append("Request.Url.Scheme");
  77.       sb.Append("");
  78.         sb.Append("" +Request.Url.Scheme + "");
  79.         sb.Append("");


  80.         // Request.Url.Host
  81.         sb.Append("");
  82.       sb.Append("Request.Url.Host");
  83.       sb.Append("");
  84.         sb.Append("" +Request.Url.Host + "");
  85.       sb.Append("");


  86.         // Request.Url.Port
  87.       sb.Append("");
  88.         sb.Append("Request.Url.Port");
  89.       sb.Append("");
  90.         sb.Append("" +Request.Url.Port + "");
  91.       sb.Append("");


  92.         // Request.Url.Authority
  93.       sb.Append("");
  94.         sb.Append("Request.Url.Authority");
  95.         sb.Append("");
  96.         sb.Append("" +Request.Url.Authority + "");
  97.       sb.Append("");


  98.         // local Request.Url.LocalPath
  99.       sb.Append("");
  100.         sb.Append("Request.Url.LocalPath");
  101.       sb.Append("");
  102.         sb.Append("" +Request.Url.LocalPath + "");
  103.       sb.Append("");


  104.         // Request.PathInfo
  105.       sb.Append("");
  106.       sb.Append("Request.PathInfo");
  107.       sb.Append("");
  108.         sb.Append("" +Request.PathInfo + "");
  109.       sb.Append("");


  110.         // Request.Url.PathAndQuery
  111.       sb.Append("");
  112.       sb.Append("Request.Url.PathAndQuery");
  113.       sb.Append("");
  114.         sb.Append("" +Request.Url.PathAndQuery + "");
  115.         sb.Append("");


  116.         // Request.Url.Query
  117.       sb.Append("");
  118.       sb.Append("Request.Url.Query");
  119.       sb.Append("");
  120.         sb.Append("" +Request.Url.Query + "");
  121.         sb.Append("");


  122.         // Request.Url.Fragment
  123.         // 原则上你应该无法从 Request.Url.Fragment 取得任何数据,因为通常 Browser 不会送出 #toc 这个部分
  124.       sb.Append("");
  125.       sb.Append("Request.Url.Fragment");
  126.       sb.Append("");
  127.         sb.Append("" + Request.Url.Fragment+ "");
  128.       sb.Append("");


  129.         // Request.Url.Segments
  130.         sb.Append("");
  131.         sb.Append("");
  132.       sb.Append("Request.Url.Segments");
  133.         sb.Append("");
  134.         sb.Append("");
  135.         string[] segments = Request.Url.Segments;
  136.         foreach (string s in segments)
  137.         {
  138.           sb.Append("" + s + "");
  139.           sb.Append("
  140. ");
  141.         }
  142.         sb.Append("");
  143.         sb.Append("");


  144.       sb.Append("");

  145.         ltlTable.Text= sb.ToString();
  146.     }
复制代码

最后编辑xelnage 最后编辑于 2008-04-22 20:03:23
分享 转发
TOP