登录 / 注册
Request.UrlReferrer,Request.Url获取数据
发布时间:2019-06-27 10:06:50 查看:4535

由WebForm1.aspx 页面 点击跳转到WebForm2.aspx页面

Request.UrlReferrer(Self)  
当前页面:值为null {http://localhost:3961/WebForm1.aspx}
    AbsolutePath: "/WebForm1.aspx" AbsoluteUri: "http://localhost:3961/WebForm1.aspx" Authority: "localhost:3961" DnsSafeHost: "localhost" Fragment: "" Host: "localhost" HostNameType: Dns
IsAbsoluteUri: true IsDefaultPort: false IsFile: false IsLoopback: true IsUnc: false LocalPath: "/WebForm1.aspx" OriginalString: "http://localhost:3961/WebForm1.aspx" PathAndQuery: "/WebForm1.aspx"  Port: 3961 Query: "" Scheme: "http" Segments: {string[2]}
    UserEscaped: false UserInfo: "" -------------------- Request.Url
{http://localhost:3961/WebForm2.aspx}
    AbsolutePath: "/WebForm2.aspx" AbsoluteUri: "http://localhost:3961/WebForm2.aspx" Authority: "localhost:3961" DnsSafeHost: "localhost" Fragment: "" Host: "localhost" HostNameType: Dns
IsAbsoluteUri: true IsDefaultPort: false IsFile: false IsLoopback: true IsUnc: false LocalPath: "/WebForm2.aspx" OriginalString: "http://localhost:3961/WebForm2.aspx" PathAndQuery: "/WebForm2.aspx" Port: 3961 Query: "" Scheme: "http" Segments: {string[2]}
UserEscaped: false UserInfo: ""

Request.url请求路径的一些属性

Request.url请求路径的一些属性 1,Request.UrlReferrer.AbsolutePath=获取URL的绝对路径
例:"/Manager/Module/OfficialManage/Issuedadocument/Issuedadocument_WorkNew.aspx" 2,Request.UrlReferrer.AbsoluteUri=获取绝对URL
例:"http://localhost:2855/Manager/Module/OfficialManage/Issuedadocument/Issuedadocument_WorkNew.aspx?FormID=1&FlowID=1" 3,Request.UrlReferrer.Authority=获取域名的主机名和端口号
例:"localhost:2855" 4,Request.UrlReferrer.DnsSafeHost(Host)=获得安全的主机名
例:"localhost" 5,Request.UrlReferrer.OriginalString=获取原始的路径
例:"http://localhost:2855/Manager/Module/OfficialManage/Issuedadocument/Issuedadocument_WorkNew.aspx?FormID=1&FlowID=1" 6,Request.UrlReferrer.PathAndQuery=获取主机绝对路径加查询条件
例:"/Manager/Module/OfficialManage/Issuedadocument/Issuedadocument_WorkNew.aspx?FormID=1&FlowID=1"  7,Request.UrlReferrer.Port=端口
例:2855 8,Request.UrlReferrer.Query=查询条件
例:"?FormID=1&FlowID=1"

asp.net获取当前页面文件名,参数,域名等方法

ASP.net后台获取当前页面的文件名  

    System.IO.Path.GetFileName(Request.Path).ToString();  获取当前页面文件名,参数,域名等方法  

    假设当前页完整地址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.test.com"是域名 "aaa"是虚拟目录名 "bbb.aspx"是页面名(文件名) "id=5&name=kelli"是参数  


    【1】获取 完整url (协议名+域名+虚拟目录名+文件名+参数)  

    string url=Request.Url.ToString();  url= http://www.test.com/aaa/bbb.aspx?id=5&name=kelli  

    【2】获取 虚拟目录名+页面名+参数:  

    string url=Request.RawUrl;  (或 string url=Request.Url.PathAndQuery;)  url= /aaa/bbb.aspx?id=5&name=kelli  

    【3】获取 虚拟目录名+页面名:  

    string url=HttpContext.Current.Request.Url.AbsolutePath;  (或 string url= HttpContext.Current.Request.Path;)  url= /aaa/bbb.aspx4】获取 域名:  

    string url=HttpContext.Current.Request.Url.Host;  url= www.test.com5】获取 参数:  

    string url= HttpContext.Current.Request.Url.Query;  url= ?id=5&name=kelli  

    Request.QueryString["id"]和Request.QueryString["name"]访问各参数  

    Request.UrlReferrer可以获取客户端上次请求的url的有关信息, 这样我们就可以通过这个属性返回到“上一页”。  

    同样地,Request.UrlReferrer.Query可以获取客户端上次请求的url的有关参数部分。  


    Request.ApplicationPath:    /testweb    
       Request.CurrentExecutionFilePath:    /testweb/default.aspx Request.FilePath:    /testweb/default.aspx Request.Path:    /testweb/default.aspx Request.PathInfo:     
       Request.PhysicalApplicationPath:    E:\WWW\testweb\    
       Request.PhysicalPath:    E:\WWW\testweb\default.aspx Request.RawUrl:    /testweb/default.aspx Request.Url.AbsolutePath:    /testweb/default.aspx Request.Url.AbsoluteUri:    http://www.test.com/testweb/default.aspx Request.Url.Host:    www.test.com Request.Url.LocalPath:    /testweb/default.aspx 

Request.UrlReferrer使用详解

Request.UrlReferrer可以获取客户端上次请求的url的有关信息。
这样我们就可以通过这个属性返回到“上一页”,
示例如下

1.首先在Page_load中获得并储存该信息

Page_load(object obj,EventArgs e) { if(!IsPostBack)
{ if(Request.UrlReferrer!=null) // {
ViewState["UrlReferrer"]=Request.UrlReferrer.ToString();
}
}
}

页面回发后会改变Request.UrlReferrer,将其指向当前页面,所以需要进行判断:只有在第一次请求页面的时候才储存该信息
因为可能“上一次”的url不存在,所以需要进行判断,只有在Request.UrlReferrer存在的情况下才进行储存

2.然后在返回函数中使用该信息

void Return()
{ if(ViewState["UrlReferrer"]!=null)
Response.Redirect(ViewState["UrlReferrer"].ToString();
}

在使用Request.UrlReferrer时还要注意:

  1. 如果上一页面使用document.location方法导航到当前页面,Request.UrlReferrer返回空值
  2. 如果有A,B两个页面,在浏览器中直接请求A页面,在A页面的中Page_Load事件中导航到B 页面,则 Request.UrlReferrer返回空。因为
    在Page_load事件中页面还未初始化,所以无法记录当前页的信息,导航到b页面也就无法获得上一页面的信息
  3. 点击刷新按钮不会改变Request.UrlReferrer
宁夏银川舜新艺软件开发
ShunXinyi Soft develop
主要经营
软件开发,网站制作,网页设计,移动应用(安卓、苹果),微信,微网站,
FLASH动画,电子商务,计算机软硬件及网络设备等。
电话:18695132945 QQ:23923027
舜新艺软件开发 宁ICP备16001093号-1 宁公网安备 64010602000809号