文章出處
文章列表
ASP.NET獲取請求的url信息匯總
最近做項目需要處理一個用代碼獲取當前網站的域名或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
文章列表
全站熱搜