Get URL and move to the next page

0

I have tried in several ways, I need to get the value of the current URL, and move to the next page, so that after the create action is completed, I go back to the URL that was passed, how can I proceed? Thank you.

I'm trying this way:

string urlAnterior = Request.Headers["Referer"].ToString();

if (urlAnterior.Contains("Pagina"))
    return RedirectToAction("");
else
    return RedirectToAction("");

And also like this:

 string urlAnterior = Request.Headers["Referer"].ToString();

    if (urlAnterior.Contains("Pessoa"))
        return RedirectToAction("Edit", "Pessoa");
    else
        return RedirectToAction("Index", "ContaReceber");

But neither worked, wanted to save the URL in a variable for example, and move to the next page, so he would know correctly, where to go back.

EDIT:

Here is where I call the create page of accounts receivable, passing the id parameter of the person who is in the edit.

<a asp-page="/ContaReceber/Create" asp-route-id="@Request.Query["id"]" class="btn btn-primary btn-sm">Criar nova Conta</a>
    
asked by anonymous 05.07.2018 / 13:24

2 answers

0

You can use UrlReferrer to get the previous url.

    //ASP.NET MVC 5
    public ActionResult SuaAction()
    {
        string urlAnterior = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
        Response.Redirect(urlAnterior); //Redireciona pra url anterior.

        return View();
    }

    //ASP.NET MVC CORE
    public IActionResult SuaAction()
    {
        string urlAnterior = Request.Headers["Referer"].ToString(); 
        Response.Redirect(urlAnterior); //Redireciona pra url anterior.

        return View();
    }
    
05.07.2018 / 13:51
0

A possible output: When calling Action / AccountReceive / Create, pass an additional parameter called returnUrl, which is the current URL:

  • In your Startup class, in the ConfigureServices method, add the line:

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    
  • In your Controller, add the property to IHttpContextAcessor and receive it in the controller:

    private IHttpContextAccessor _accessor;
    public Controller(IHttpContextAccessor accessor) {
        this._acessor = accessor;
    }
    
  • Now in your method, you can access the current URL:

    var url = _accessor.HttpContext?.Request?.GetDisplayUrl();

  • In the method where you get the current URL, use RedirectToAction by passing the:

    public IActionResult Index(string returnUrl = "") { ViewBag.ReturnUrl = returnUrl; }

Now, in the Action Account view you can access Razor's past url with @ ViewBag.ReturnUrl and create a button to return to the previous page.

    
05.07.2018 / 14:32