c# - 显示错误页面 MVC

标签 c# asp.net-mvc

我在我的 Global.asax 文件中使用 Application_Error 方法捕获未处理的错误,这是我目前所拥有的:

 protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Response.Clear();

        HttpException httpException = exception as HttpException;

        if(httpException != null)
        {
            string action;

            switch(httpException.GetHttpCode())
            {
                case 404:
                    action = "404";
                    break;
                case 500:
                    action = "500";
                    break;

                default:
                    action = "Other";
                    break;
            }

              Server.ClearError();

              Response.Redirect(String.Format("~/Error/?error=" + action + "&message=" + exception.Message));
        }
    }

但是我真的不喜欢将用户重定向到错误页面的想法,事实上我希望 URL 保持不变。

例如,当页面不存在时,它不应重定向到 URL 中的路径,而应保留在同一页面上,但仍会显示错误。

有人知道怎么做吗?

最佳答案

我的解决方案与您的重定向非常相似,但根据您的部署方式,我会通过在 IIS 中自定义错误页面本身来实现。

Check Out this answered question

关于c# - 显示错误页面 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206523/

相关文章:

c# - 是否可以确定进程运行了多长时间

c# - 我最终采用了什么设计模式?

c# - 如何在 asp.net mvc 4 中创建具有局部 View 的搜索功能

c# - 根据条件禁用 html 按钮

c# - Asp.Net Core 中的 SSL 不起作用

c# - WebClient 很慢

c# - 部分 View 验证错误在部分 View 上获取触发 View 获取调用本身

javascript - Kendo mvc Grid ClientTemplate javascript 函数不起作用

c# - ASP.NET MVC/C# : Can I avoid repeating myself in a one-line C# conditional statement?

jquery - jquery 重定向后在 asp.net mvc 中查找引用页面