28

From my client detail page I have a button to edit the client record which redirects to an edit page. I have a "return to client detail" link on the edit page which I want to redirect the user back to the previous client detail page.

<a asp-controller="Client" asp-action="Detail" asp-route-id="@Model.ClientID">Return to client detail</a>

Currently this works as expected but takes extra time as it reloads the detail page from scratch (ie running all the various db queries again). Since the user is really just cancelling the edit without any changes to the state of the client I am wanting to return the user to the previous detail page without having to go through the controller action again.

Essentially I am wanting to simulate the browser back button (to improve responsiveness) but i'm not sure how to implement this or whether it's good practice to do so. Some guidance would be appreciated.

Thanks

2
  • 1
    on click of the link execute javascript history.go(-1). This will take the browser one step back in the history. Commented Feb 14, 2017 at 10:34
  • So what is the problem with running your queries again? Commented Feb 14, 2017 at 10:42

8 Answers 8

33

For IActionResult you can use this code:

public IActionResult Test()
{
    return Redirect(Request.Headers["Referer"].ToString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can now use return Redirect(Request.Headers.Referer.ToString());
25

U know what? I hate JS so i will write answer with backend side. The HTTP referer is an HTTP header field that identifies the address of the webpage that linked to the resource being requested. So simply read that and pass to view (always remember about XSS and validation, user can easly spoof HTTP request)

In action controller

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

In view (razor)

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

2 Comments

i tried this and although it all hooks together ok it doesn't solve the problem because when clicking the hyperlink the respective controller action is stilled called
I don't really like JS either, but it is so much simpler with it, that it makes perfect sense to prefer it over backend solution, it also enables you to do something like refer to site previous to previous etc
24

You can use

<a href='javascript:history.go(-1)'>Return to client detail</a>

or onclick

<a href="##" onClick="history.go(-1); return false;"> Return to client detail</a> 

1 Comment

I have the same problem as the poster - I want to go back from a detail to a ("heavy") search page WITHOUT RELOAD THE FULL SEARCH Page. All the solutions on this page do a FULL Reload (same as if the user simply click on the browser back button). Is there no solution to go back WITHOUT reloading the full page...?
3

It should be like this

<input type="button" onclick= "history.go(-1)" value="Return to client detail" />

Comments

1

One note of caution using Request.Headers["Referer"] - if someone refreshes the destination page for some reason, Request.Headers["Referer"] will be empty. Using history.go(-1) gives the expected behavior despite page refresh.

Comments

1

I would never rely on my button, thinking a user will prefer it to browser back button.

I would say the correct way to solve this problem is to store the page state somewhere, for example, save ViewModel in TempData or Session. Then, if exists, load from it, instead of running db queries. It's quick and reliable.

Comments

0

I think that you need to get rid of the idea of passing through the controller. If you need to browse quickliest with asp net core code about href you can try this.

 <a asp-area="" onclick="history.go(-1);">Return to client detail</a>

Comments

0

This Request.Headers["Referer"] will not work if the user refresh the page or the page is been loaded twice, which mean, clicking back will not take you out of the current page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.