asp.net-mvc-3returnurl

What is ReturnURL in mvc3


What is Return URL in mvc3? When i write down my url in adress bar of the browser, at the append return url is automatically appended to it. How this happnes?

I provide the following url in adress bar

http://localhost:55875/admin

and after pressing enter it becomes

http://localhost:55875/Account/Logon?ReturnUrl=%2fadmin

I have debugged the logic for Logon action method, but dont see any logi which is appending returnurl to the provided url? How did this happen?


Solution

  • When an unauthenticated user tries to get into a section of your application which requires authentication, then returnUrl comes into the picture.The Url requested by the unauthenticated user is basically stored in returnurl.

    for example below controller decorated with Authorize attribute :

    [Authorize]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }
    

    The login action grabs the value of this parameter and puts it in the ViewBag so it can be passed to the View. The View then stores this value in the form as shown by this line of code in the View.

    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
    

    The reason it is stored in the View is so that when the user does a Submit after entering their user name and password, the controller action that handles the post back will have access to this value.