mar*_*nes 12 error-handling asp.net-mvc ioc-container asp.net-mvc-3
我在这里发现了另一个问题,它提供了我的问题的第一部分的解决方案,即如何在ASP.NET MVC中生成404页面而不重定向到另一个页面(因此最初请求的URL保留在地址栏中).
ASP.NET MVC - 如何抛出类似于StackOverflow上的404页面
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
Response.Clear();
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpException != null)
{
routeData.Values.Add("action", httpException.GetHttpCode() == 404 ? "NotFound" : "Unknown");
// clear the error, otherwise, we will always get the default error page.
Server.ClearError();
// call the controller with the route
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不使用IOCControllerFactory,404方面的工作对我来说很好,但是一旦我使用工厂,不使用错误代码global.asax,而是我得到以下错误:
IControllerFactory'MyNamespace.IOCControllerFactory'没有为名称'blah'返回控制器.
我如何解决这个问题,而不是试图摆脱我的controllerfactory,并为我的每个控制器创建一个无参数构造函数?
Mar*_*ark 12
ASP.NET 可以做到这一点OOTB(从3.5开始).在<CustomErrors>元素添加:redirectMode=ResponseRewrite
更新
我发布了错误的代码.IIS 7可以处理这个问题.
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" path="/home/notfound" responseMode="ExecuteURL" />
</httpErrors>
<system.webServer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6011 次 |
| 最近记录: |