[英]Customer Error in Asp.net MVC web.config
in my MVC web application I need a 404 page for all error. 在我的MVC Web应用程序中,我需要一个404页面来处理所有错误。 I have editing web.config file which this line:
我正在编辑以下行的web.config文件:
<customErrors mode="On">
<error redirect="/error/404" statusCode="404" />
</customErros>
It'work, but in browser url I see URL/error/404. 可以,但是在浏览器网址中我看到的是URL / error / 404。 How I can see the called URL?
如何查看被叫URL?
Thanks. 谢谢。
The customErrors
node is actually older than httpErrors
so try this instead. customErrors
节点实际上早于httpErrors
因此请尝试执行此操作。 Make sure you have these nodes in your web.config
as children of the system.webServer
node: 确保在
web.config
中将这些节点作为system.webServer
节点的子节点:
<httpErrors>
<error statusCode="404" path="/error/404" responseMode="ExecuteURL" />
</httpErrors>
Alternatively you can do this two ways with customErrors
: First is to amend the individual error
node, the second is to change the customErrors
node. 或者,您可以通过
customErrors
做到这两种方式:第一种是修改单个error
节点,第二种是更改customErrors
节点。 I can't remember if one of these is now obsolete, however feel free to try both. 我不记得其中一个是否已过时,但是请随意尝试两者。
<error statusCode="404" path="/error/404" responseMode="ExecuteURL"/>
or 要么
<customErrors mode="On" redirectMode="ResponseRewrite">
If the above aren't working something else would be up. 如果上述方法无效,则可能会出现其他情况。 Have you checked the IIS Logs?
您是否检查了IIS日志?
Links 链接
httpErrors
Documentation httpErrors
文档
Custom Errors Element documentation (Archived) 自定义错误元素文档 (已存档)
I partial solve using Application_error in global.asax. 我在global.asax中使用Application_error部分解决。 I added this method
我添加了这种方法
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
if (httpException != null && httpException.GetHttpCode() == 404)
{
Response.Clear();
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
IController controller = new CommonController();
var routeData = new RouteData();
routeData.Values.Add("controller", "Common");
routeData.Values.Add("action", "PageNotFound");
var requestContext = new RequestContext(
new HttpContextWrapper(Context), routeData);
controller.Execute(requestContext);
}
}
This work fine. 这项工作很好。 The problem is from controller: .ie if I don't find a db record, I want return a 404 error with default error page.
问题出在控制器:.ie,如果我找不到数据库记录,我想返回带有默认错误页面的404错误。 I want also show in browser the original called url, not the redirect url.
我还想在浏览器中显示原始URL,而不是重定向URL。 Using return View();
使用return View(); in controller, this not happen.
在控制器中,这不会发生。 How I can solve it?
我该如何解决? Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.