在我的MVC应用程序有机会处理之前,如何阻止IIS7推出自己的404?

时间:2022-09-25 12:36:31

I have an ASP.NET MVC 2 application, which has an Application_Error event handler in global.asax. In this, I'm detecting the case where the Exception type is HttpException and the HTTP code is 404, and redirecting to my own 404-handling page.

我有一个ASP.NET MVC 2应用程序,它在global.asax中有一个Application_Error事件处理程序。在这里,我正在检测Exception类型是HttpException并且HTTP代码是404的情况,并重定向到我自己的404处理页面。

This works fine on my Cassini development server, but now I'm trying to move it over to my production server which has IIS7 (using integrated mode).

这在我的Cassini开发服务器上工作正常,但现在我正在尝试将其移动到具有IIS7的生产服务器(使用集成模式)。

When I request a non-existent URL, IIS7 is showing its own 404 page, and so far as I can tell, my Application_Error method is never called.

当我请求一个不存在的URL时,IIS7显示了自己的404页面,据我所知,我的Application_Error方法永远不会被调用。

How do I fix that?

我该如何解决这个问题?

5 个解决方案

#1


6  

I answered that in another post: ASP.NET Application hosted on IIS7 that is ignoring custom errors and falls back to IIS errors

我在另一篇文章中回答:在IIS7上托管的ASP.NET应用程序忽略了自定义错误并回退到IIS错误

"To disable the IIS error messages you have to set

“要禁用必须设置的IIS错误消息

  Response.TrySkipIisCustomErrors = true;

in your error page. After that, your Error messages should show without problem."

在您的错误页面中。之后,您的错误消息应显示没有问题。“

#2


4  

Can't you just turn off the 404 httpError:

你不能只关闭404 httpError:

    <httpErrors errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
    </httpErrors>

#3


0  

In ASP.NET MVC I use a custom ErrorController with an Index method (default handler) and a few custom handlers (401, 404) using the following web.config settings

在ASP.NET MVC中,我使用带有Index方法的自定义ErrorController(默认处理程序)和一些使用以下web.config设置的自定义处理程序(401,404)

<!--  CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable. 
Add <error> tags for each of the errors you want to handle.   
"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running 
on the local Web server. This setting is recommended for security purposes, so 
that you do not display application detail information to remote clients. -->
<customErrors mode="Off" defaultRedirect="~/Error">
    <error statusCode="401" redirect="~/Error/Unauthorized" />
    <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

I don't use Application_Error except for logging purposes.

除了日志记录之外,我不使用Application_Error。

#4


0  

This is discussed in the MVC overview tutorials. It is probably the the case that your IIS 7 is set up in Classic mode. See:

这在MVC概述教程中讨论。可能是您的IIS 7设置为经典模式的情况。看到:

Using ASP.NET MVC with Different Versions of IIS (C#)

使用ASP.NET MVC与不同版本的IIS(C#)

#5


0  

The Answer by Flynn solved my issue of ASP.NET taking control of the 404

Flynn的回答解决了我的ASP.NET控制404的问题

Now I am able to do this:

现在我能够这样做:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
Response.Write("<script type='text/javascript'>setTimeout(function () { window.location = '/'; }, 1000)</script>");
HttpContext.Current.ApplicationInstance.CompleteRequest();

#1


6  

I answered that in another post: ASP.NET Application hosted on IIS7 that is ignoring custom errors and falls back to IIS errors

我在另一篇文章中回答:在IIS7上托管的ASP.NET应用程序忽略了自定义错误并回退到IIS错误

"To disable the IIS error messages you have to set

“要禁用必须设置的IIS错误消息

  Response.TrySkipIisCustomErrors = true;

in your error page. After that, your Error messages should show without problem."

在您的错误页面中。之后,您的错误消息应显示没有问题。“

#2


4  

Can't you just turn off the 404 httpError:

你不能只关闭404 httpError:

    <httpErrors errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
    </httpErrors>

#3


0  

In ASP.NET MVC I use a custom ErrorController with an Index method (default handler) and a few custom handlers (401, 404) using the following web.config settings

在ASP.NET MVC中,我使用带有Index方法的自定义ErrorController(默认处理程序)和一些使用以下web.config设置的自定义处理程序(401,404)

<!--  CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable. 
Add <error> tags for each of the errors you want to handle.   
"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running 
on the local Web server. This setting is recommended for security purposes, so 
that you do not display application detail information to remote clients. -->
<customErrors mode="Off" defaultRedirect="~/Error">
    <error statusCode="401" redirect="~/Error/Unauthorized" />
    <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

I don't use Application_Error except for logging purposes.

除了日志记录之外,我不使用Application_Error。

#4


0  

This is discussed in the MVC overview tutorials. It is probably the the case that your IIS 7 is set up in Classic mode. See:

这在MVC概述教程中讨论。可能是您的IIS 7设置为经典模式的情况。看到:

Using ASP.NET MVC with Different Versions of IIS (C#)

使用ASP.NET MVC与不同版本的IIS(C#)

#5


0  

The Answer by Flynn solved my issue of ASP.NET taking control of the 404

Flynn的回答解决了我的ASP.NET控制404的问题

Now I am able to do this:

现在我能够这样做:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
Response.Write("<script type='text/javascript'>setTimeout(function () { window.location = '/'; }, 1000)</script>");
HttpContext.Current.ApplicationInstance.CompleteRequest();