博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何通过HttpRequest对象获取路径和URL片段?
阅读量:5046 次
发布时间:2019-06-12

本文共 7082 字,大约阅读时间需要 23 分钟。

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

转载于:https://www.cnblogs.com/mopno1/archive/2012/01/30/2332026.html

你可能感兴趣的文章
关于Redis处理高并发
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
asp.net core 系列 16 Web主机 IWebHostBuilder
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
iOS 数组排序
查看>>
第三节
查看>>
PHP结合MYSQL记录结果分页呈现(比较实用)
查看>>
Mysql支持的数据类型
查看>>
openSuse beginner
查看>>
Codeforces 620E(线段树+dfs序+状态压缩)
查看>>
Windows7中双击py文件运行程序
查看>>
Market entry case
查看>>
bzoj1230 开关灯 线段树
查看>>
LinearLayout
查看>>
学习python:day1
查看>>
css3动画属性
查看>>
第九次团队作业-测试报告与用户使用手册
查看>>