Search code examples
asp.net-mvcback

In MVC, do I have to implement a custom error filter to redirect the user back from an error page?


For usability reasons, I have the requirement that an error page should tell the user something on the lines of

Sorry, we could not find this product in our database. You can go back to the previous page or search for a different product.

where the go back link is supposed to provide the same functionality as the back button.

I tried implementing it using Request.UrlReferrer, but it was a null in the smoke test. I found an answer here on SO which says that the browser can be set to not send it and

Bottom line is that you shouldn't trust it. Just append the url to the GET string and redirect based off that.

But as this view is displayed by using the default exception handling filter of MVC, I cannot append this in my code, as it does not send the user to the view in the first place.

Do I have to write my own exception handling filter if I want this functionallity, or is there a simpler way to do it? If I have to write it, what are pitfalls to look out for? It is possible that we will drop the link requirement if this means a substantial increase in the complexity of our code and just let the users click their own back button, but I don't have the experience to make an estimation.


Solution

  • I recommend solving this with JavaScript. Try

    window.history.back()
    

    As the onclick event for your anchor tag.