Share via


Can I set the returnUrl path?

Question

Thursday, January 18, 2018 8:56 AM

Good morning.

I am creating a homepage using MVC.

I am using the authentication change as an individual user account.

When I run it using the login controller that is automatically created, I log in successfully and then return to the home screen.

The path address is ~ /Home, can you change this address?

Can I specify a returnUrl?

thank you

All replies (3)

Friday, January 26, 2018 6:33 AM âś…Answered

Hi slkim,

Hi, oned_gk!

Where can I change it?

thank you.

As far as I know, the return url is pass as parameter to the login action.

Locate the Login.cshtml:

            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

So if you want to change the login url , you just need set the ViewBag.ReturnUrl in the login view.

More details, you could refer to below codes:

@using TestMVC.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
    ViewBag.ReturnUrl = "~/Home/About";
}

<h4>@ViewBag.Title.</h4>
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
....

Best Regards,

Brando


Thursday, January 18, 2018 9:05 AM

Yes you can, encode the target url


Friday, January 19, 2018 1:11 AM

Hi, oned_gk!

Where can I change it?

thank you.