locked
Back to previous page - ASP.NET CORE RRS feed

  • Question

  • User706413455 posted

    I am trying to go back to the previous page after the request, but it is not working, it follows what I am trying to do:

    string urlAnterior = Request.Headers["Referer"].ToString();
    
        if (urlAnterior.Contains("Pagina"))
            return RedirectToAction("");
        else
            return RedirectToAction("");

    I tried this way too:

      string urlAnterior = Request.Headers["Referer"].ToString();
    
            if (urlAnterior.Contains("Pessoa"))
                return RedirectToAction("Edit", "Pessoa");
            else
                return RedirectToAction("Index", "ContaReceber");

    How can I go back to the page where the page was called?

    Wednesday, July 4, 2018 7:55 PM

Answers

  • User-1764593085 posted

    Hi marianac_costa,

    You could get last url by using Request.Headers["Referer"].ToString() which you have tried before and use Redirect() to reach your page.

    If you would like to use it in a page like an Edit page which has Get and Post methods, you could refer to below demo:

    1.Add returnUrl in the get method.

    public async Task<IActionResult> Edit(int? id)
    {     
         ...
         ... 
         ViewBag.returnUrl = Request.Headers["Referer"].ToString();
         return View(customer);
    }
    

    2.Pass the returnUrl parameter from the Model to the [HttpPost] method using a hidden field in the form:

    @using (Html.BeginForm())
    {
      <input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />
      ...
      ...
    }
    

    3.In the [HttpPost] method we pull the parameter from the hidden field and Redirect to it.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id,string returnUrl,Customer customer)
    {
      ...
    
      if (ModelState.IsValid)
      {
        ...
        ...    
        return Redirect(returnUrl);                         
      }
    
       return View(customer);
    }
    

    You could also place previous url in your model and refer below link which is related in asp.net MVC.

    https://stackoverflow.com/questions/9772947/c-sharp-asp-net-mvc-return-to-previous-page

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 6, 2018 3:01 AM

All replies

  • User-1764593085 posted

    Hi marianac_costa,

    I am not sure how you want to definite the previous page. Take the Edit page as an example, in case you have saved the value, and whether you want to redirect to index page( A different page) or stay in Edit Page( HttpGet method) to see the results you have changed. You could explain your requirement.

    Basically , you could use return Redirect(urlAnterior) directly redirect to the page whose route is the value of urlAnterior.

    Best Regards,

    Xing

    Thursday, July 5, 2018 8:34 AM
  • User706413455 posted

    But how do I get this url? I can not get her value, nor even redirect it.

    Thursday, July 5, 2018 11:17 AM
  • User82362805 posted

    try "request.Scheme + "://" + request.Host.Value"  to get the url

    Thursday, July 5, 2018 12:57 PM
  • User-1764593085 posted

    Hi marianac_costa,

    You could get last url by using Request.Headers["Referer"].ToString() which you have tried before and use Redirect() to reach your page.

    If you would like to use it in a page like an Edit page which has Get and Post methods, you could refer to below demo:

    1.Add returnUrl in the get method.

    public async Task<IActionResult> Edit(int? id)
    {     
         ...
         ... 
         ViewBag.returnUrl = Request.Headers["Referer"].ToString();
         return View(customer);
    }
    

    2.Pass the returnUrl parameter from the Model to the [HttpPost] method using a hidden field in the form:

    @using (Html.BeginForm())
    {
      <input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />
      ...
      ...
    }
    

    3.In the [HttpPost] method we pull the parameter from the hidden field and Redirect to it.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id,string returnUrl,Customer customer)
    {
      ...
    
      if (ModelState.IsValid)
      {
        ...
        ...    
        return Redirect(returnUrl);                         
      }
    
       return View(customer);
    }
    

    You could also place previous url in your model and refer below link which is related in asp.net MVC.

    https://stackoverflow.com/questions/9772947/c-sharp-asp-net-mvc-return-to-previous-page

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 6, 2018 3:01 AM
  • User-1780421697 posted

    On target page you can use client side script to get previous url by  

    document.referrer   OR  window.history.previous.href

    then you can check the condition and redirect user back if condition is satisfied by

    window.location.replace(http://abc.com); OR window.location.href = "http://abc.com";



    Friday, July 6, 2018 11:07 AM
  • User-2101441342 posted

    thank you! 

    Tuesday, July 21, 2020 12:31 PM
  • User-474980206 posted

    Browsers only set refer when navigation is done via a link. That is why you must track if a post.

    Tuesday, July 21, 2020 2:41 PM