Use JavaScript and C # To obtain a referer,
1. JavaScript
Copy codeThe Code is as follows:
/**
* Obtain the Referer of an HTTP request
* @ Ishost: whether to return the Host (website Homepage Address) if the Boolean Referer type is null)
*/
Function get_http_referer (ishost ){
If (ishost = undefined) {ishost = true ;}
If (document. referrer ){
Return document. referrer;
} Else {
If (ishost ){
Return window. location. protocol + "//" + window. location. host;
} Else {
Return "";
}
}
}
2. C #
Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the Referer of the HTTP request
/// </Summary>
/// <Param name = "ishost"> whether to return the Host (website Homepage Address) when the Referer is empty </param>
/// <Returns> string </returns>
Public string GetReferer (bool ishost)
{
If (Request. UrlReferrer! = Null)
{
Return Request. UrlReferrer. ToString ();
}
Else
{
If (ishost)
{
Return Request. Url. Scheme + ": //" + Request. Url. Authority;
}
Else
{
Return "";
}
}
}
3. Obtain the parts of the Request URL in C #
URL: http: // localhost: 1897/News/Press/Content. aspx/123? Id = 1 # toc
Copy codeThe Code is as follows:
Request. ApplicationPath/
Request. PhysicalPath D: \ Projects \ Solution \ web \ News \ Press \ Content. aspx
System. IO. Path. GetDirectoryName (Request. PhysicalPath) D: \ Projects \ Solution \ web \ News \ Press
Request. PhysicalApplicationPath D: \ Projects \ Solution \ web \
System. IO. Path. GetFileName (Request. PhysicalPath) Content. aspx
Request. CurrentExecutionFilePath/News/Press/Content. aspx
Request. FilePath/News/Press/Content. aspx
Request. Path/News/Press/Content. aspx/123
Request. RawUrl/News/Press/Content. aspixels/123? Id = 1
Request. Url. AbsolutePath/News/Press/Content. aspx/123
Request. Url. AbsoluteUri http: // localhost: 1897/News/Press/Content. aspx/123? Id = 1
Request. Url. Scheme http
Request. Url. Host localhost
Request. Url. Port 1897
Request. Url. Authority localhost: 1897
Request. Url. LocalPath/News/Press/Content. aspx/123
Request. PathInfo/123.
Request. Url. PathAndQuery/News/Press/Content. aspx/123? Id = 1
Request. Url. Query? Id = 1
Request. Url. Fragment
Request. Url. Segments/
News/
Press/
Content. aspx/
123
4. JavaScript window. location object
Http://www.bkjia.com/article/57407.htm
How do I get the page referer in javascript and C? If you have any questions, contact me.