Back to previous page - mvc page Razor

Asked

Viewed 661 times

1

I’m trying to get back to the previous page this way:

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

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

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

Code 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 this is where I call inside the Receivables Index:

<a asp-page="Create" class="btn btn-primary">Criar nova Conta</a>

2 answers

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 on 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 Send 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 do it like this

No control can also use so

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

E in the view (Razor)

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

    It does not work for me, I use mvc page Razor. I tried to edit according to my need, but it does not return to the previous page.

  • I’ve put together some solutions that might work but I’d need more details to see what’s wrong.

  • I tried some that would suit my scenario, but it didn’t work out, here’s the thing, I have the Edit page people, and the index page receivables, within the two I call to create receivables, I want that when it comes from within the Edit of people, it goes back to this page after including, and if it’s come from the index accounts receivable, after including I want it to go back to the index of accounts receivable.

  • if (Request.Urlreferrer == "url index") {Return view("view name") } Something like this ?

  • I tried to do something like: if (idpessoa == 0) { Return Redirecttopage("./Index"); } Else { Return Redirecttopage("/Person/Edit?id=" + idpessoa); } but.

1


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

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 origin of the request that can be two, you can solve by changing your original code so:

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

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

Changing controller and method names only.

  • It gives me the following error, which I try this way: Invalidoperationexception: No route Matches the supplied values. Debugging the code, I was able to verify that in the urlAnterior it takes the value: http://localhost:55695/Contareceber/Create? id=2. Even the request coming from the Pessoa/Edit page.

  • the error occurs because it did not find the route:On route Matches the supplied values, I changed the line of Redirecttoaction.

  • you are using Actionlink? could put the code you use to call the create, contained in the view.

  • I updated with the flame of the create page. I tried the way Voce reported, of the two ways it returns to http://localhost:55695/Contareceber

  • I made a demo here, and modified the Tagshelpers of the links, I used the Tagshelpers Asp-controller and Asp-action, so the urlAnterior comes with the correct previous link. <a Asp-controller="Count" Asp-action="Create" Asp-route-id="@Request.Query[" id "]">Create new Account</a>

  • I did the same, it always returns to the Index of Accounts receivable

  • still coming the same url in the urlAnterior variable?

  • Still continues, it does not redirect to where it should, it always returns to the index of accounts receivable.

  • but after the modifications, the urlAnterior keeps coming the same?

  • No page named './People/Edit? id=3' Matches the supplied values. I am trying to pass through, but it returns this error.

  • can you put this code on github? check in your Startup.Cs file if the routes are configured.

Show 6 more comments

Browser other questions tagged

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