Request.UrlReferrer null?
If you use the standard Membership provider, and set the Authorization for the directory/page, the code will automatically set a query parameter of ReturnUrl and redirect after a successfull login.
If you don't want to use the Membership provider pattern, I would suggest manually doing the query string parameter thing as well. HTTP referrers are not very reliable.
Request.UrlReferrer is coming as null on page refresh
UrlReferrer
can't be controlled by your server side code - it just exposes value sent by browser/client along with request. There are plenty of cases that value is not present (initial request/refresh, HTTP/HTTPS transition) so your code must be able to handle this case.
At very least you just need to check for null
(you may also need to handle UriFormatException
if you need to handle requests from rogue clients in a nice way):
if (Request.UrlReferrer != null)
{
// run your code that deals with referrer
}
Trying to check if urlreferrer is null
Request.UrlReferrer is null if there is no referrer, which makes your reference to Request.UrlReferrer.AbsolutePath (a property on a null object) throw a null reference exception.
Instead, try;
@if (Request.UrlReferrer == null)
Request.UrlReferrer not giving expected result
It turned out to be my misunderstanding of the referrer policy "strict-origin". It is too restrictive on internal referrals. What I needed was the slightly less strict "same-origin".
You can see this excellent article which explains all.
Related Topics
Determine If Current Application Is Activated (Has Focus)
Asp .Net MVC Disable Client Side Validation at Per-Field Level
How to Prevent Windows from Entering Idle State
C# How to Test a File Is a Jpeg
Meanings of Declaring, Instantiating, Initializing and Assigning an Object
Possible to Iterate Backwards Through a Foreach
Calculated Column in Ef Code First
How to Get Urls of Open Pages from Chrome and Firefox
Use of SQLparameter in SQL Like Clause Not Working
Why Is It Considered Bad to Expose List<T>
Datatrigger Does Not Change Text Property
Why Cannot Ienumerable<Struct> Be Cast as Ienumerable<Object>
Mongodb Gridfs with C#, How to Store Files Such as Images
Regarding Local Variable Passing in Thread
C#:Is Variance (Covariance/Contravariance) Another Word for Polymorphism