Request.UrlReferrer Chinese messy code solution

  • 2020-05-19 04:34:32
  • OfStack

Refer to most of the network solutions, no one can fix, if the end of the road, try the following method:
Divide the URL of the first page into two segments, encode the following parameters (it is not ok to encode URL directly), and then combine 1.
 
if(!Page.IsPostBack) 
{ 
ReUrl = Page.Request.UrlReferrer.ToString().Split('?')[0].ToString() + HttpUtility.UrlEncode(Page.Request.UrlReferrer.Query,System.Text.Encoding.GetEncoding("GB2312")); 
if (ReUrl == null || ReUrl == "") 
{ 
ReUrl = HttpContext.Current.Request.Url.PathAndQuery ; 
} 
ViewState["ReUrl"] = ReUrl; 
} 

Also note that you need to decode when you return to the previous page
Response.Redirect(Server.UrlDecode((string)ViewState["ReUrl"].ToString()));
An important piece of code was found in the reference solution, especially for sites with high security.

1. When the user changes the parameters of URL in the address bar, it is indicated that the user is not entitled to perform this operation
Place the following code in the Page_Load event
 
try 
{ 
string strTemp = Request.UrlReferrer.PathAndQuery ; 
} 
catch 
{ 
throw new Exception(" You can't change the parameters !") ; 
} 

Note: this method is a "side-door" method that takes advantage of the fact that if the user modifies URL's parameters, Request.UrlReferrer
You're going to get null,null, of course you can't have PathAndQuery anymore, so you're going to throw an exception

Related articles: