简体   繁体   中英

How to get the previous url in C#?

How to get the previous url in a MasterPage in C# ?

I'm trying to find the page which is redirected from.

Thanks in advance.

You can get information of the previous url with the UrlReferrer property. This works in MVC and Web forms.

Request.UrlReferrer.AbsoluteUri

Note that in the first page the property Request.UrlReferrer will be null. Also, it will be null if a redirection occurs (eg when a user logs into the web page).

This property is based on the HTTP_REFERER variable, so you could use this one instead.

Request.ServerVariables["HTTP_REFERER"]

Since the HTTP_REFERER is a variable sent by the client it might be altered or removed by the request. Also, the variable is not set when the referre url starts with https .

This article mentions a few points why the Request.UrlReferrer can be null.

Usually you use a query string parameter to achieve this: current?previousUrl=/some/11 .

This will allow you to access this value from the server-side code using Context.Request.QueryString["previousUrl"] in your master page code-behind.

string urlName = Request.UrlReferrer.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM