在MVC中,我是否必须实现自定义错误过滤器以将用户从错误页面重定向回来?

时间:2021-07-01 03:16:09

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

我尝试使用Request.UrlReferrer实现它,但它在冒烟测试中是空的。我在SO上找到了一个答案,它说浏览器可以设置为不发送它

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

底线是你不应该相信它。只需将网址附加到GET字符串并重定向即可。

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.

但是,由于使用MVC的默认异常处理过滤器显示此视图,我无法在我的代码中附加此视图,因为它不会将用户首先发送到视图。

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.

如果我想要这个功能,我是否必须编写自己的异常处理过滤器,或者有更简单的方法吗?如果我必须写它,有什么缺陷需要注意?如果这意味着我们的代码的复杂性大幅增加并且让用户点击他们自己的后退按钮,我们可能会删除链接要求,但我没有经验来进行估算。

1 个解决方案

#1


1  

I recommend solving this with JavaScript. Try

我建议用JavaScript解决这个问题。尝试

window.history.back()

As the onclick event for your anchor tag.

作为锚标记的onclick事件。

#1


1  

I recommend solving this with JavaScript. Try

我建议用JavaScript解决这个问题。尝试

window.history.back()

As the onclick event for your anchor tag.

作为锚标记的onclick事件。