C#-获取customErrors页面的状态代码

C#-获取customErrors页面的状态代码,c#,asp.net,C#,Asp.net,在我的web.config文件中,我打开了customErrors并默认重定向到我的控制器ErrorController,如下所示: <customErrors mode="On" defaultRedirect="/Error/"> </customErrors> 但它总是返回200,因为它重定向到一个状态代码为200的页面……当我转到一个不存在的页面时,我仍然得到一个200代码而不是404。我只想有一个页面,获取错误代码,而不是有多个错误页面。这可能吗?删除defa

在我的web.config文件中,我打开了customErrors并默认重定向到我的控制器ErrorController,如下所示:

<customErrors mode="On" defaultRedirect="/Error/">
</customErrors>

但它总是返回200,因为它重定向到一个状态代码为200的页面……当我转到一个不存在的页面时,我仍然得到一个200代码而不是404。我只想有一个页面,获取错误代码,而不是有多个错误页面。这可能吗?

删除
defaultRedirect
并使用
global.asax.cs

输入如下代码

[ValidateInput(false)]
        protected void Application_Error()
        {

             //prevent the errors coming back!
             this.Context.ClearError();

             //redirect
             this.Context.Response.Redirect($"~/Home/Error");

        }
错误代码可能在
this.Context
的某个地方,请使用debug并尝试查找它


只有一个供参考的
this.Context.AllErrors
包含所有错误,因此可以向您显示错误所在。

System.Web.HttpContext.Current.Response.StatusCode;当它应该是404时仍然返回200,因为我试图转到一个不存在的页面……我可以从应用程序_错误中获取状态代码并将其传递给我的错误控制器吗,这就是我要做的。对不起,我没说清楚。
[ValidateInput(false)]
        protected void Application_Error()
        {

             //prevent the errors coming back!
             this.Context.ClearError();

             //redirect
             this.Context.Response.Redirect($"~/Home/Error");

        }