• 如何通过HttpRequest对象获取路径和URL片段?


    原文标题:How Do I Get Paths and URL fragments from the HttpRequest object?
    原文地址:http://www.cambiaresearch.com/articles/53/how-do-i-get-paths-and-url-fragments-from-the-httprequest-object

    摘要

    如果你编写过许多ASP.NET程序,那么你可能花了很多时间将Url映射到物理磁盘上的某个位置。当你需要在服务器上保存文件,或者执行某些URL解析、或者将请求重新路由的时候,你都需要做这些工作。

    这篇文章将要解释Request对象的一些属性,这些属性根据应用程序和当前请求来提供路径和url信息。

    首先,这里有两张表来展示针对给出的请求路径,Request对象的相关属性的值。

    由于某些我也不清楚的原因,Url.Fragment属性通常是空的,而不是包含"#fragment"的值。

    请求路径: http://localhost:96/Cambia3/Temp/Test.aspx?q=item#fragment

    Request.ApplicationPath: /Cambia3
    Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
    Request.FilePath: /Cambia3/Temp/Test.aspx
    Request.Path: /Cambia3/Temp/Test.aspx
    Request.PathInfo:  
    Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
    Request.RawUrl: /Cambia3/Temp/Test.aspx?query=arg
    Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx
    Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx?query=arg
    Request.Url.Fragment:  
    Request.Url.Host: localhost
    Request.Url.Authority: localhost:96
    Request.Url.LocalPath: /Cambia3/Temp/Test.aspx
    Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx?query=arg
    Request.Url.Port: 96
    Request.Url.Query: ?query=arg
    Request.Url.Scheme: http
    Request.Url.Segments: /
    Cambia3/
    Temp/
    Test.aspx

    请求的路径: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment

    Request.ApplicationPath: /Cambia3
    Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
    Request.FilePath: /Cambia3/Temp/Test.aspx
    Request.Path: /Cambia3/Temp/Test.aspx/path/info
    Request.PathInfo: /path/info
    Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
    Request.RawUrl: /Cambia3/Temp/Test.aspx/path/info?query=arg
    Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx/path/info
    Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg
    Request.Url.Fragment:  
    Request.Url.Host: localhost
    Request.Url.LocalPath: /Cambia3/Temp/Test.aspx/path/info
    Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx/path/info?query=arg
    Request.Url.Port: 96
    Request.Url.Query: ?query=arg
    Request.Url.Scheme: http
    Request.Url.Segments: /
    Cambia3/
    Temp/
    Test.aspx/
    path/
    info

    下面是我用C#写的一个方法,用来处理URL并且生成表格。你需要在aspx页面中加入一个ID为"lblOutput"的Label控件,然后使用这个方法。

      1 private void DisplayRequestObjectProperties()
    2 {
    3 lblOutput.Text = "<table cellpadding=2 border=1>";
    4
    5 lblOutput.Text += "<tr><td colspan=2 align=center>";
    6 lblOutput.Text += "Some HttpRequest path and ULR properties:";
    7 lblOutput.Text += "</td></tr>";
    8
    9 // application path
    10 lblOutput.Text += "<tr><td>";
    11 lblOutput.Text += "Request.ApplicationPath:";
    12 lblOutput.Text += "</td><td>";
    13 lblOutput.Text += "<b>" + Request.ApplicationPath + "</b>";
    14 lblOutput.Text += "</td></tr>";
    15
    16 // current execution file path
    17 lblOutput.Text += "<tr><td>";
    18 lblOutput.Text += "Request.CurrentExecutionFilePath:";
    19 lblOutput.Text += "</td><td>";
    20 lblOutput.Text += "<b>" + Request.CurrentExecutionFilePath + "</b>";
    21 lblOutput.Text += "</td></tr>";
    22
    23 // file path
    24 lblOutput.Text += "<tr><td>";
    25 lblOutput.Text += "Request.FilePath:";
    26 lblOutput.Text += "</td><td>";
    27 lblOutput.Text += "<b>" + Request.FilePath + "</b>";
    28 lblOutput.Text += "</td></tr>";
    29
    30 // path
    31 lblOutput.Text += "<tr><td>";
    32 lblOutput.Text += "Request.Path:";
    33 lblOutput.Text += "</td><td>";
    34 lblOutput.Text += "<b>" + Request.Path + "</b>";
    35 lblOutput.Text += "</td></tr>";
    36
    37 // path info
    38 lblOutput.Text += "<tr><td>";
    39 lblOutput.Text += "Request.PathInfo:";
    40 lblOutput.Text += "</td><td>";
    41 lblOutput.Text += "<b>" + Request.PathInfo + "</b>";
    42 lblOutput.Text += "</td></tr>";
    43
    44 // physical application path
    45 lblOutput.Text += "<tr><td>";
    46 lblOutput.Text += "Request.PhysicalApplicationPath:";
    47 lblOutput.Text += "</td><td>";
    48 lblOutput.Text += "<b>" + Request.PhysicalApplicationPath + "</b>";
    49 lblOutput.Text += "</td></tr>";
    50
    51 // raw url
    52 lblOutput.Text += "<tr><td>";
    53 lblOutput.Text += "Request.RawUrl:";
    54 lblOutput.Text += "</td><td>";
    55 lblOutput.Text += "<b>" + Request.RawUrl + "</b>";
    56 lblOutput.Text += "</td></tr>";
    57
    58 // absolute path
    59 lblOutput.Text += "<tr><td>";
    60 lblOutput.Text += "Request.Url.AbsolutePath:";
    61 lblOutput.Text += "</td><td>";
    62 lblOutput.Text += "<b>" + Request.Url.AbsolutePath + "</b>";
    63 lblOutput.Text += "</td></tr>";
    64
    65 // absolute uri
    66 lblOutput.Text += "<tr><td>";
    67 lblOutput.Text += "Request.Url.AbsoluteUri:";
    68 lblOutput.Text += "</td><td>";
    69 lblOutput.Text += "<b>" + Request.Url.AbsoluteUri + "</b>";
    70 lblOutput.Text += "</td></tr>";
    71
    72 // fragment
    73 lblOutput.Text += "<tr><td>";
    74 lblOutput.Text += "Request.Url.Fragment:";
    75 lblOutput.Text += "</td><td>";
    76 lblOutput.Text += "<b>" + Request.Url.Fragment + "</b>";
    77 lblOutput.Text += "</td></tr>";
    78
    79 // host
    80 lblOutput.Text += "<tr><td>";
    81 lblOutput.Text += "Request.Url.Host:";
    82 lblOutput.Text += "</td><td>";
    83 lblOutput.Text += "<b>" + Request.Url.Host + "</b>";
    84 lblOutput.Text += "</td></tr>";
    85
    86 // authority
    87 lblOutput.Text += "<tr><td>";
    88 lblOutput.Text += "Request.Url.Authority:";
    89 lblOutput.Text += "</td><td>";
    90 lblOutput.Text += "<b>" + Request.Url.Authority + "</b>";
    91 lblOutput.Text += "</td></tr>";
    92
    93 // local path
    94 lblOutput.Text += "<tr><td>";
    95 lblOutput.Text += "Request.Url.LocalPath:";
    96 lblOutput.Text += "</td><td>";
    97 lblOutput.Text += "<b>" + Request.Url.LocalPath + "</b>";
    98 lblOutput.Text += "</td></tr>";
    99
    100 // path and query
    101 lblOutput.Text += "<tr><td>";
    102 lblOutput.Text += "Request.Url.PathAndQuery:";
    103 lblOutput.Text += "</td><td>";
    104 lblOutput.Text += "<b>" + Request.Url.PathAndQuery + "</b>";
    105 lblOutput.Text += "</td></tr>";
    106
    107 // port
    108 lblOutput.Text += "<tr><td>";
    109 lblOutput.Text += "Request.Url.Port:";
    110 lblOutput.Text += "</td><td>";
    111 lblOutput.Text += "<b>" + Request.Url.Port + "</b>";
    112 lblOutput.Text += "</td></tr>";
    113
    114 // query
    115 lblOutput.Text += "<tr><td>";
    116 lblOutput.Text += "Request.Url.Query:";
    117 lblOutput.Text += "</td><td>";
    118 lblOutput.Text += "<b>" + Request.Url.Query + "</b>";
    119 lblOutput.Text += "</td></tr>";
    120
    121 // scheme
    122 lblOutput.Text += "<tr><td>";
    123 lblOutput.Text += "Request.Url.Scheme:";
    124 lblOutput.Text += "</td><td>";
    125 lblOutput.Text += "<b>" + Request.Url.Scheme + "</b>";
    126 lblOutput.Text += "</td></tr>";
    127
    128 // segments
    129 lblOutput.Text += "<tr><td>";
    130 lblOutput.Text += "Request.Url.Segments:";
    131 lblOutput.Text += "</td><td>";
    132 string[] segments = Request.Url.Segments;
    133 foreach (string s in segments)
    134 lblOutput.Text += "<b>" + s + "</b><br>";
    135 lblOutput.Text += "</td></tr>";
    136
    137 lblOutput.Text += "</table>";
    138 }



  • 相关阅读:
    2.Servlet基础
    3.AOP入门1
    1.Tomcat配置
    Linq学习(一)-初涉Linq
    Linq学习(零)-错误汇总
    Intellij IDEA14配置
    大数据插入Excel报错处理
    VS插件-Resharper
    SVN异常处理(五)-状态小图标不见了
    SVN系列学习(四)-TortoiseSVN其他操作
  • 原文地址:https://www.cnblogs.com/mopno1/p/2332026.html
Copyright © 2020-2023  润新知