【发布时间】:2019-09-30 13:48:22
【问题描述】:
假设您的应用程序抛出了一个不在ControllerActionInvoker.InvokeAction 方法范围内的异常。
这可能发生,例如,如果用户输入了错误的 URL,该 URL 不能解析为任何现有控制器或操作的名称。
http://www.example.com/doesNotExist
该 URL 会抛出一个HttpException,状态码为 404。
然后您可以在 Application 对象的 Error 事件处理程序中处理它。
// Global.asax.cs
public void Application_Error()
{
HttpException exception = Server.GetLastError() as HttpException;
// do whatever with it
}
从这里开始,是否可以在不实际将用户重定向到将呈现视图的新 URL 的情况下呈现 ASP.NET MVC 视图?
// Global.asax.cs
public void Application_Error()
{
HttpException exception = Server.GetLastError() as HttpException;
// can I *render* a View here?
// In other words, can I execute a ViewResult here?
// Can I get back into MVC from here?
// But without redirecting the user. Just re-writing the Response object?
}
我在这里看到的所有示例都只是在 Response 对象上设置了 HTTP StatusCode 和 StatusDescription。我想从这里返回View而不将用户重定向到查看网址。
我正在使用面向 .NET Framework 4.6.1 的 ASP.NET MVC 5.2.6。
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-5