我在这里发现了另一个问题,它提供了我问题的第一部分的解决方案,即如何在ASP中生成404页面。. NET MVC WITHOUT重定向到另一个页面(所以最初请求的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));
}
}
404方面的事情对我来说很好,如果我不使用IOCControllerFactory,但只要我做使用工厂,错误代码在global.asax
不使用,而是我得到以下错误:
IControllerFactory的MyNamespace。IOCControllerFactory'没有返回名称为'blah'的控制器。
我如何解决这个问题,而不诉诸于摆脱我的controllerfactory,并为我的每个控制器创建一个无参数的构造函数?
NET可以做到这一点(从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>