简体   繁体   English

Asp.net MVC web.config中的客户错误

[英]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.

相关问题 Web.config文件ASP.NET MVC中出现双重错误 - Double error in Web.config file ASP.NET MVC ASP.NET 5 MVC 6中的web.config - web.config in ASP.NET 5 MVC 6 ASP.NET MVC 5 web.config <system.web> 编译和httpRuntime错误 - ASP.NET MVC 5 web.config <system.web> compilation & httpRuntime error 覆盖web.config ASP.NET MVC中的ConnectionString - Overwrite ConnectionString in web.config ASP.NET MVC ASP.NET MVC 4 web.config文件中的LDAP身份验证 - LDAP Authentication in ASP.NET MVC 4 web.config file asp.net mvc4中的web.config中的Windows身份验证 - Windows Authentication in web.config in asp.net mvc4 我如何解决此错误,MVC 4 中 asp.net 的 web.config 中不允许使用“connectionStringName”属性 - How can i resolve this error the 'connectionStringName' attribute is not allowed in web.config of asp.net in MVC 4 来自ASP.NET MVC4中Web.config文件的连接字符串错误 - Connection Strings error from the Web.config File in asp.net mvc4 C#ASP.NET MVC web.config和MachineToApplication错误 - C# ASP.NET MVC web.config and MachineToApplication error 达到文件限制时如何返回错误? 上传 + asp.net mvc 3 + web.config - How to get an error back when file limit is reached? Plupload + asp.net mvc 3 + web.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM