【发布时间】:2012-10-18 19:07:20
【问题描述】:
我正在编写一个新的 IHttpModule。我想使用 BeginRequest 事件处理程序使某些带有 404 的请求无效。如何终止请求并返回 404?
【问题讨论】:
-
这有你的答案:stackoverflow.com/questions/499817/… -- 抛出一个 HttpException
标签: c# asp.net httpmodule
我正在编写一个新的 IHttpModule。我想使用 BeginRequest 事件处理程序使某些带有 404 的请求无效。如何终止请求并返回 404?
【问题讨论】:
标签: c# asp.net httpmodule
你可以试试
throw new HttpException(404, "File Not Found");
【讨论】:
Response.StatusCode = 404; Response.End() 更好?
您可以执行以下操作:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Location", l_notFoundPageUrl);
HttpContext.Current.Response.Status = "404 Not Found";
HttpContext.Current.Response.End();
将 l_notFoundPageUrl 分配给您的 404 页面。
【讨论】:
Response.StatusCode = 404; 还不够吗?
您可以将状态码显式设置为 404,例如:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.End();
响应将停止执行。
【讨论】:
另一种可能是:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
【讨论】: