Last post Feb 04, 2018 07:52 PM by Dev72
Feb 03, 2018 07:46 PM|Dev72|LINK
I have an application using ASP.NET Core 2 MVC and Razor views, with some minimal jQuery. What I have trouble with is preserving contents from previous screens. I believe in older versions of ASP.NET, the ViewState was used for this? So imagine having some
result rendered on a screen, user navigates to next screen, and presses back button. In my current implementation the initial controller is called and screen is rendered from scratch without any content. I would expect that e.g. pressing browser back button
the previous screen results would be present (from browser cache?). How could I achieve this? There is some information on state management for Core, but these all require you to manually manage screen-specific state. This really feels like I'm trying to over-engineer
something that is obviously simple for someone else :)
Feb 03, 2018 10:35 PM|maherjendoubi|LINK
In action controller
if(Request.Headers["Referer"] != null)
ViewData["Reffer"] = Request.Headers["Referer"].ToString();
In view (razor)
<a href="@ViewData["Reffer"]">Return to client detail</a>
Feb 04, 2018 08:44 AM|Dev72|LINK
Thanks for fast response! Could you maybe explain a little bit what is happening in the above? Also, is the point here that it works with the browser back button itself? Wouldn't that raise the popup in the browser asking if user want's to do a re-post?
Also, can the ASP.NET Core ResponseCaching be used here? Isn't that used to store the content in browser cache?
Feb 04, 2018 12:38 PM|maherjendoubi|LINK
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.
Feb 04, 2018 03:07 PM|mgebhard|LINK
The PRG pattern helps with this situation.
I believe in older versions of ASP.NET, the ViewState was used for this?
No, ViewState persists data between post backs.
I would expect that e.g. pressing browser back button the previous screen results would be present (from browser cache?). How could I achieve this?
The back button either loads content from browser cache or requests content from the server if the page is NOT cached. The results depends on how your dealing with cache. If you are doing nothing then the PRG pattern will help quite a bit as well as alleviating
the pesky re-post alert.
Controlling browser cache in ASP Core.
Use the browser dev tools (F12) to view how the browser is handling the back button.
If cache is turned off and the PRG pattern is implemented then the browser will do an HTTP GET to an action method when the back button is clicked. It is up to you to make sure the content is rendered as expected. This could mean saving state in a DB or
tweaking the code base. You'll need to work out the details.
Feb 04, 2018 04:55 PM|Dev72|LINK
Is browser cache an option here then, using the ResponseCache and Cache-Control? I'm not sure how the browser cache works. I.e. if I would use ResponseCache, would all my new operations on a page update the browser cache, or would the data be stale for x
minutes? A normal use-case would be like this:
Feb 04, 2018 07:29 PM|mgebhard|LINK
Is browser cache an option here then, using the ResponseCache and Cache-Control? I'm not sure how the browser cache works. I.e.
That's what the docs are for.
if I would use ResponseCache, would all my new operations on a page update the browser cache, or would the data be stale for x minutes?
It sounds like user entry so the cache would pertain to users. Or you can simple tell the browser to not cache at all. The decision to use cache is up to you. I simply let you know that it is possible to configure how browsers cache in the event you want
granular control over the content.
user performs some actions on page 'A' that retrieve data to page from database
user navigates to next page 'B' user clicks back button, therefore navigating back to page 'A' (previous action results are displayed on page)
user performs new actions on page 'A' that retrieve new set of data to page from database
(cache is updated and not stale)
As stated above, use the PRG pattern. That will solve most of the issues. Next determine if you want to turn off cache and purposefully load content from the server when the user clicks the back button or you want the data to load from browser cache if
available. Either way, this is a design issue that you need to figure out. For example, turn cache off for Page A and force the browser to always do a GET. Build a few tests and pick the approach that meets your requirements.
Feb 04, 2018 07:52 PM|Dev72|LINK
Alright, thanks. If the browser cache does get updated in the event that the user changes data previously entered, then it sounds like that would be the easier route to go for back navigation. Anyway, I will test both options.