Home > Software engineering >  get currentUrl from current view and pass it to controller to redirect user back to the URL he comes
get currentUrl from current view and pass it to controller to redirect user back to the URL he comes

Time:02-28

How to pass the ReturnUrl from View to the following controller, Using Asp.MVc.Core.6

 public IActionResult SetLanguage(string culture, string returnUrl)
{
   
    var _returnUrl = returnUrl;

    Response.Cookies.Append(
        CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
        new CookieOptions {Expires = DateTimeOffset.UtcNow.AddYears(1)}
    );

    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
        return LocalRedirect(_returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
    }

The view

enter image description here

enter image description here

CodePudding user response:

You can simply use

asp-route-returnUrl="@Context.Request.Path"

It will do the trick

  • Related