Reputation: 1089
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
Upvotes: 27
Views: 47764
Reputation: 139
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.
Upvotes: 0
Reputation: 529
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.
Upvotes: 1
Reputation: 21
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.
Upvotes: 1
Reputation: 1494
For IActionResult
you can use this code:
public IActionResult Test()
{
return Redirect(Request.Headers["Referer"].ToString());
}
Upvotes: 32
Reputation: 2747
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>
}
Upvotes: 25
Reputation: 1031
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>
Upvotes: 0
Reputation: 195
It should be like this
<input type="button" onclick= "history.go(-1)" value="Return to client detail" />
Upvotes: 3
Reputation: 2058
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>
Upvotes: 23