Back to previous page - mvc page razor

0

I'm trying to get back to the previous page, like this:

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

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

But it returns me to the current updated page, I need to go back to the previous page, how to proceed?

Code that I use to call create inside people:

<div class="form-group">
  <a asp-page="/ContaReceber/Create" asp-route-id="@Request.Query[" id "]" class="btn btn-primary btn-sm">Criar nova Conta</a>
</div>

And here is where I call within the Accounts Receivable Index:

<a asp-page="Create" class="btn btn-primary">Criar nova Conta</a>
    
asked by anonymous 03.07.2018 / 16:56

2 answers

1

The RedirectToAction expects as argument a string with the name of the controller you want to redirect to, as it is blank, it redirects to the controller of the current method. Then you should enter the name of your controller:

return RedirectToAction("SuaController");

Or if you want to call the previous Url, do so:

return Redirect(urlAnterior);

I saw in your comments that you want to redirect to a page, based on the source of the request, which can be two, you can solve by changing your original code like this:

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

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

Changing only the names of controllers and methods.

    
03.07.2018 / 21:25
1

try this way

or so

return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
return Redirect(Request.UrlReferrer.ToString());
return Redirect(HttpContext.Request.Headers["Referer"].ToString());

or so from the View side

<a href='javascript:history.go(-1)'>Go Back</a>
<a href="##" onClick="history.go(-1); return false;"> Go Back</a> 
<input type='button' onclick='history.go(-1);' value='Go Back' />
<input type="button" value="GO BACK" onclick="location.href='@Request.UrlReferrer'" />

@Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer})

If you send the url by parameter you can also use this way

public ActionResult FilterData(string redirectUrl = null)
{
    // Do some work
    // ....
    if (redirectUrl != null) {
        return this.Redirect(redirectUrl);
    }

    return View("Default");
}

You can also do this

The control can also use this

if(Request.Headers["Referer"] != null)
{
    ViewData["Reffer"] = Request.Headers["Referer"].ToString();
}

And in the view (razor)

@if(!string.IsNullOrWhiteSpace(ViewData["Reffer"]))
{
    <a href="@ViewData["Reffer"]">Return to client detail</a>
}
    
03.07.2018 / 17:07