I'm looking for a quick, easy and reliable way of getting the browser's HTTP Referrer in ASP.Net (C#). I know the HTTP Referrer itself is unreliable, but I do want a reliable way of getting the referrer if it is present.
相关问题
- Angular RxJS mergeMap types
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
Sometime you must to give all the link like this
(in option when "Current" not founded)
You could use the UrlReferrer property of the current request:
This will read the Referer HTTP header from the request which may or may not be supplied by the client (user agent).
Like this: HttpRequest.UrlReferrer Property
Use the
Request.UrlReferrer
property.Underneath the scenes it is just checking the
ServerVariables("HTTP_REFERER")
property.Explanation
The
Request.UrlReferer
will throw aSystem.UriFormatException
if the referer HTTP header is malformed (which can happen since it is not usually under your control).As for using
Request.ServerVariables
, per MSDN:Request.ServerVariables Collection
Request.Headers Property
Request.Headers
is a better choice thanRequest.ServerVariables
, sinceRequest.ServerVariables
contains all of the environment variables as well as the headers, whereRequest.Headers
is a much shorter list that only contains the headers.So the best solution is to use the
Request.Headers
collection to read the value directly. Do heed Microsoft's warnings about HTML encoding the value if you are going to display it on a form, though.