HttpContext.Request.UrlReferrer not the referrer

Last post 05-01-2008 6:38 AM by cvallance. 11 replies.

Sort Posts:

  • HttpContext.Request.UrlReferrer not the referrer

    03-28-2008, 9:56 PM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    I've got a user control on every page that is used to log a user in. This user control posts to the login action on the user controller (this action also renders a view if the credentials fail). But when ever I try and get the referrer from the httpContext.Request.UrlReferrer object, I simply get the url that I have posted to, e.g. I get '/user/login' rather than the page they posted from.

    I could just have a hidden input in the login form that holds the current url's value, but surely I should be able to get it from the UrlReferrer object...? Is this a bug, or am I missing something?

    My code looks like the following:

    public void LogIn(string ReturnUrl)
    {
        if (!string.IsNullOrEmpty(ReturnUrl)) //passed in from GET or POST
        {
            ViewData["returnurl"] = ReturnUrl;
        }
        else //Not passed in
        {
            ViewData["returnurl"] = HttpContext.Request.UrlReferrer.ToString();
        }

        blah blah blah

    }

    Thanks.

  • Re: HttpContext.Request.UrlReferrer not the referrer

    03-29-2008, 4:20 AM
    • Loading...
    • wordracr
    • Joined on 01-24-2008, 5:05 AM
    • Posts 95

    you have that code inside a user control, right?
    I tried the same thing and I get what you do.. UrlReferrer only returns the site and current path.  Same thing when doing HttpContext.Current.Request...

    But, doing the code in an actual page will be fine - the page is returned as well.  Maybe just do that and pass that into the control via a custom string property.
  • Re: HttpContext.Request.UrlReferrer not the referrer

    03-29-2008, 4:49 AM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    wordracr:
    you have that code inside a user control, right?

    No, that code is part of the login action on the user controller... the login user control (.ascx) only deals with the rendering of the login form.

    wordracr:
    But, doing the code in an actual page will be fine - the page is returned as well.  Maybe just do that and pass that into the control via a custom string property.
     

    I'm sorry, I don't quite follow you there? Do you mean do it in the code behind for the view?

    Cheers,
    Charles
     

  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-13-2008, 2:03 PM
    • Loading...
    • mogadanez
    • Joined on 09-22-2005, 9:33 AM
    • Posts 34
    I have exactly same problem
  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-28-2008, 3:49 PM
    • Loading...
    • bzurer
    • Joined on 12-07-2005, 12:41 AM
    • Posts 18

     I am having the same issue. I have scoured the web on this to no avail. Has anyone figured it out or does anyone have a good workaround?

     

    Thanks 

    Filed under:
  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-28-2008, 5:08 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 111
    • AspNetTeam

    I cannot reproduce this issue on my machine.  If somebody can create a minimal app where this bug shows up, I'll look into it.

  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-30-2008, 1:41 PM

    If I understand your question correctly, this is not a bug and is the correct behaviour.

    I interpreted your query to be as follows:

    1. Your initial call is to: controller/originalurl  -->redirected to login
    2. controller/login      (referrer= originalurl)
    2.  b.  User control displays <form> pointing to ProcessLogin Action
    2.  c  User submits form
    3. controller/ProcessLogin   (referrer=login)

     This state at point 3 for the referrer is correct.

    You will need to keep the original url referrer between requests for the processlogin to pick it up.
    You have two options that come to mind:
    1. Hidden field on the form can be picked up in the ProcessLogin (as you suggested yourself)
    2. TempData storage... you can use the ViewContext.TempData dictionary to store between controller requests - it lasts for the lifetime of the next request.

    I would suggest option 1 as it is simple, ensures a stateless environment and is not subject to errors or unexpected behaviour on application recycle.

    Regards,
    foreachbiscuit
    blog @ http://foreachbiscuit.wordpress.com
    Filed under:
  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-30-2008, 9:13 PM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    levib:
    If somebody can create a minimal app where this bug shows up, I'll look into it.

    Hi Levib, I'll try and do that tonight for you to have a look at. Thanks!

  • Re: HttpContext.Request.UrlReferrer not the referrer

    04-30-2008, 9:30 PM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    Hi foreachbiscuit,

    Thats kinda / not really the application flow - it's like that, but forget about step 1. With your version, the bug should appear in step 3 and the referrer won't be 'login' but will in fact be 'ProcessLogin'... is this the case?

    My situation involves a login box / form that sits on every single page and points to a login action. When a user posts this form to the login action the UrlReferrer inside the login action equals 'Login' not the page it was posted from.

    Follow? If you could just confirm what UrlReferrer equals at step 3 in your process, that could clear things up.

    Yes, I've already implemented a hidden field that tells me the original page. I still feel there is a bug in the Request.UrlReferrer... at least last time I checked on the first and second CTP's.

    Regards,
    Charles
     


     

  • Re: HttpContext.Request.UrlReferrer not the referrer

    05-01-2008, 3:52 AM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    levib:
    If somebody can create a minimal app where this bug shows up, I'll look into it.

    Right, so if you create the default asp.net mvc application. Then add the following to the index.aspx view for the home controller (with the appropriate Imports)

     

    <% using (Html.Form<HomeController>(h => h.About(), FormMethod.Post))
    { %> <input type="submit" name="Submit" value="submit" />

    <% } %>

      

    Then the about action on the home controller should look like the following

      

    1            public void About()
    2 {
    3 string urlReferrer = HttpContext.Request.UrlReferrer.ToString();
    4 5 RenderView("About");
    6 }
     

    Now put a break point on line 5 and debug. When you hit the submit button on the front page the urlReferrer string will be something like "http://localhost:port/Home/About" BUT I would expect it to be "http://localhost:port/Home/Index" or "http://localhost:port/" or something along those lines.

    Thanks for your help Levib. Regards, Charles. 

    Filed under:
  • Re: HttpContext.Request.UrlReferrer not the referrer

    05-01-2008, 4:14 AM
    Answer
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 111
    • AspNetTeam

    Got it.  This was a bug in System.Web.Abstractions.dll where we were accidentally returning the current request's Url in the UrlReferrer property.  The bug has since been fixed, and if you download the latest Codeplex release (04/16), you shouldn't encounter this problem.  You can download the new drop from http://www.codeplex.com/aspnet.

    Sorry for the inconvenience.

  • Re: HttpContext.Request.UrlReferrer not the referrer

    05-01-2008, 6:38 AM
    • Loading...
    • cvallance
    • Joined on 03-20-2008, 5:45 AM
    • Posts 27

    levib:
    The bug has since been fixed, and if you download the latest Codeplex release (04/16), you shouldn't encounter this problem.
     

    Ah ha, I actually hadn't tested it since I downloaded the CTP 3 preview and made the last test using the CTP2 template rather than the new template. Wink

    Thanks levib.

Page 1 of 1 (12 items)