Grab URL and skip to the next page

Asked

Viewed 1,868 times

2

I have tried several ways, I need to take the value of the current URL, and move to the next page, so that after the action of the create 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 so:

 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 return.

EDIT:

This is where I call the create page of accounts receivable, passing the id parameter of the person in Edit.

<a asp-page="/ContaReceber/Create" asp-route-id="@Request.Query["id"]" class="btn btn-primary btn-sm">Criar nova Conta</a>
  • Like you’re trying to do?

  • I edited the question.

  • Unable to use the HTTP REFERER header?

  • I’m new to the language, what do you mean HTTP REFERER header? If you can help me.

  • You want to go back to a previous page of the same controller ?

  • I have the CREATE page of Accounts Receivable, this page can be called inside the Edit of people, or else from the INDEX of Accounts Receivable, I want it to return, from where it was called.

  • 1

    O Cabecalho http_referer (yes, it’s spelled "referer" and not "referrer" because the blessed one who created it misspelled the name) it serves to get the previous (or source) url of a request. When you open the page directly (or do a redirect show), you obviously won’t have anything relevant in HTTP_REFER

  • By way of curiosity: Referer or Referrer?

  • the gambiara title you can also receive your URL as parameter of your CREATE (get) action and store in a Tempdata and CREATE POST only recover the Tempdata variable ...

  • @Wallacemaxters so in my case I can’t use http referer? That’s it ?

  • @Marconciliosouza, in the case of create, if it is called in people I already pass a parameter, I would pass along ? Is that it? I’ll put in the question, what do I call in people.

  • 1

    Young, @marianac_costa, ever thought about going to url what do you want as a parameter in GET? (I did it yesterday in an implementation here)

  • @Wallacemaxters I edited the question with the part I already pass a parameter, how would I pass the url ? Sorry, I’m still learning this language.

  • 1

    @marianac_costa watch out for what is using the tool to not use wrong. The snippet code (</>) are for full HTML/JS/CSS examples. To whichever something else, use the button {}, or just indent the code with 4 spaces in the question. Even if it’s only HTML, CSS or JS that there’s nothing to execute, do not use the snippet. Tip: any other language like this, use {}.

  • @marianac_costa, have you tried Urlreferrer ? It is a property that returns just the previous url. Note my answer below.

Show 10 more comments

2 answers

1

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();
    }
  • I did exactly this way, and it returns me the current url, it is returning: { http://localhost:55695/Contareceber/Create? id=2 }, which in this case is the page I pass when I click create, inside people.

0

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

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

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    
  • In your Controller, add the property to Ihttpcontextor and receive 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 take the current URL, use Redirecttoaction by passing the parameter:

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

Okay, now in the Action Contareceber view, you can access the url you passed in Razor with @Viewbag.Returnurl and create a button to return to the previous page.

  • I’d like it to redirect automatically after create, in case an if, and redirect itself, not a button that goes back to the previous page.

  • Then you give a Redirect(returnUrl);

  • I use Asp.net core, public Controller(Ihttpcontextaccessor accessor) { not understood, and it uses the page’s own controller.

  • It is for you to put the controller constructor that will call the creation page, as I do not know the name of your classes, I put Controller. If you already have a constructor, just add the parameter.

  • I could not, I thought to do this way, Redirect Map("./Person/Edit?id=" + id); but it returns me error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.