ASP.NET获取请求的url信息汇总
2015-06-08 20:34
603 查看
ASP.NET获取请求的url信息汇总
最近做项目需要处理一个用代码获取当前网站的域名或ip信息的问题,于是尝试了ASP.NET中各种获取url信息的方法,在此总结一下:
在Global.asax文件中的 Application_BeginRequest 方法中,加入以下代码,利用日志文件记录各种方法得到的信息
logger 是定义的一个基于log4net的日志助手
Common.LogHelper 日志助手类 定义
请求url为:http://localhost:13877/NewsList-18.aspx?t=1&s=1 时的日志输出结果:
分类总结一下:
获得完全路径(在浏览器中地址栏的url):Request.Url、Request.Url.AbsoluteUri、Request.Url.OriginalString
相对网站的虚拟路径(带请求参数):Request.Url.RawUrl、Request.Url.PathAndQuery
相对网站的虚拟路径(不带请求参数):Request.FilePath、Request.Path、Request.Url.AbsolutePath、Request.Url.LocalPath
仅获取请求参数信息:Request.Url.Query
最近做项目需要处理一个用代码获取当前网站的域名或ip信息的问题,于是尝试了ASP.NET中各种获取url信息的方法,在此总结一下:
在Global.asax文件中的 Application_BeginRequest 方法中,加入以下代码,利用日志文件记录各种方法得到的信息
HttpApplication app = sender as HttpApplication; logger.Debug("Request.ApplicationPath:" + app.Request.ApplicationPath); logger.Debug("Request.FilePath:" + app.Request.FilePath); logger.Debug("Request.Path:" + app.Request.Path); logger.Debug("Request.PathInfo:" + app.Request.PathInfo); logger.Debug("Request.PhysicalApplicationPath:" + app.Request.PhysicalApplicationPath); logger.Debug("Request.PhysicalPath:" + app.Request.PhysicalPath); logger.Debug("Request.RawUrl:" + app.Request.RawUrl); logger.Debug("Request.Url:" + app.Request.Url); logger.Debug("Request.Url.AbsolutePath:" + app.Request.Url.AbsolutePath); logger.Debug("Request.Url.AbsoluteUri:" + app.Request.Url.AbsoluteUri); logger.Debug("Request.Url.Authority:"+app.Request.Url.Authority); logger.Debug("Request.Url.Fragment:" + app.Request.Url.Fragment); logger.Debug("Request.Url.Host:" + app.Request.Url.Host); logger.Debug("Request.Url.LocalPath:" + app.Request.Url.LocalPath); logger.Debug("Request.Url.OriginalString:" + app.Request.Url.OriginalString); logger.Debug("Request.Url.PathAndQuery:" + app.Request.Url.PathAndQuery); logger.Debug("Request.Url.Query:" + app.Request.Url.Query); logger.Debug("Request.Url.Segments:"); foreach (string item in app.Request.Url.Segments) { logger.Debug(item+"\t"); }
logger 是定义的一个基于log4net的日志助手
Common.LogHelper 日志助手类 定义
请求url为:http://localhost:13877/NewsList-18.aspx?t=1&s=1 时的日志输出结果:
分类总结一下:
获得完全路径(在浏览器中地址栏的url):Request.Url、Request.Url.AbsoluteUri、Request.Url.OriginalString
相对网站的虚拟路径(带请求参数):Request.Url.RawUrl、Request.Url.PathAndQuery
相对网站的虚拟路径(不带请求参数):Request.FilePath、Request.Path、Request.Url.AbsolutePath、Request.Url.LocalPath
仅获取请求参数信息:Request.Url.Query
相关文章推荐
- Note on <Pro ASP.NET MVC 4> - 01
- [Asp.net 5] Options-配置文件之后的配置
- asp 数据表分页显示
- ASP.NET MVC DropdownList 使用
- ASP.NET MVC 必备开发环境
- Aspose Cells 控件如何实现数据过滤(附代码和下载地址)
- 浅谈ASP.Net ProcessPostData方法
- aspx页面Page_Load和aspx页面上控件Page_Load事件执行顺序
- Aspose Cells 控件如何实现数据过滤(附代码和下载地址)
- Asp.Net页面生命周期
- .aspx页面 加入js文件后,js文件中文出现乱码
- asp.net DataTable 转换 excel 下载
- Asp.net Socket客户端(远程发送和接收数据)
- Asp.net中的一个判断session是否合法的做法
- ASP.NET Boilerplate Zero启动方式
- 关于在asp.net中的调试
- 【ASP.NET北大青鸟】-总结(二)
- ASP.NET WebForm Form表单如何实现MVC那种“自动装配”
- 自定义ASP.NET Identity(三)- 实现自定义MySQL ASP.NET Identity存储提供程序
- 服务中没有ASP.net State service 状态服务 (ASP.NET state server)