【发布时间】:2012-01-01 11:51:04
【问题描述】:
我正在尝试正确处理并返回此 URL 的 404:http://localhost:2867/dd./xml(注意斜线前的点)
在我当前的实现中,我在 Application_Error 中得到 4 个异常/错误。 Server.GetLastError() 返回的第一个异常是 System.Web.HttpException 而接下来的三个是 null。
我做了一个最低限度的实现来重现这个问题。这是 global.asax.cs 中的代码:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Generic");
routeData.Values.Add("area", "");
IController errorController = new ErrorController();
// this line throws System.Web.HttpException is a view is returned from ErrorController
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
错误控制器如下所示:
public class ErrorController : Controller
{
public ActionResult Generic()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
// returning content rather than a View doesn't fire 'System.Web.HttpException' in Application_Error
//return Content("Some error!");
}
}
有两个问题。一种是对于给定的 URL,而不是 Application_Error 中的一个错误,我得到 3 或 4,另一种是当从 ErrorController 返回视图时,Application_Start 中的 Execute 调用行会引发异常。如果返回一个 Content("something") 而不是这个内部(我假设是 MVC)异常不会被触发。
要查看问题,您必须处于调试模式并使用开发服务器。使用 IIS 或 IIS Express 时,由于某种原因未捕获到错误。此外,这些错误有时会消失。要让它回到开始,你必须清理溶液。
如果您想使用它,这是最基本的解决方案:http://dl.dropbox.com/u/16605600/InvalidUrl.zip
感谢您的帮助!
【问题讨论】:
标签: asp.net-mvc-3 error-handling httpexception file-not-found