我正在写一个新的ihttpmodule。我想使用BeginRequest
事件处理程序将某些请求与404无效。如何终止请求并返回404?
您可以将状态代码明确设置为404,例如:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.End();
响应将停止执行。
您可以尝试
throw new HttpException(404, "File Not Found");
另一种可能性是:
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.
您可以执行以下操作:
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页。