在我们的内部网中,有一些与href指向我的applcation的碰撞。当点击任何链接并浏览器重定向时,ELMAH会在下面生成数百个重复错误,持续10秒左右。
System.Web.HttpException: Server cannot set status after HTTP headers have been sent.
Generated: Tue, 13 Dec 2016 17:20:43 GMT
System.Web.HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.
at System.Web.HttpResponse.set_StatusCode(Int32 value)
at System.Web.Mvc.HandleErrorAttribute.OnException(ExceptionContext filterContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
我的应用程序在Application_BeginRequest()和Application_Error()中设置了以下代码,用于设置状态代码。我不确定它是否与它有任何关系。
protected void Application_BeginRequest(object sender, EventArgs e) {
Response.BufferOutput = true;
//enforce HTTPS
if (!HttpContext.Current.Request.IsLocal && !Request.IsSecureConnection )
{
bool addHttpsAttribute = true;
foreach (Filter filter in GlobalFilters.Filters)
{
if (filter.Instance is RequireHttpsAttribute)
{
addHttpsAttribute = false;
break;
}
}
if (addHttpsAttribute)
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
}
}
protected void Application_Error(object sender, EventArgs e) {
Response.BufferOutput = true;
var httpContext = HttpContext.Current;
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
var currentController = " ";
var currentAction = " ";
if (currentRouteData != null) {
if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) {
currentController = currentRouteData.Values["controller"].ToString();
}
if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) {
currentAction = currentRouteData.Values["action"].ToString();
}
}
var ex = Server.GetLastError();
var controller = new myApp.Controllers.ErrorController();
var routeData = new RouteData();
var action = "OtherError";
if (ex is HttpException) {
var httpEx = ex as HttpException;
switch (httpEx.GetHttpCode()) {
case 404:
action = "NotFound";
break;
// others if any
default:
action = "OtherError";
break;
}
}
httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = ex is HttpException ? ((HttpException) ex).GetHttpCode() : 500;
httpContext.Response.TrySkipIisCustomErrors = true;
routeData.Values["controller"] = "Error";
routeData.Values["action"] = action;
controller.ViewData.Model = new myApp.Models.HandleErrorInfo(ex, currentController, currentAction);
((IController) controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
Context.ApplicationInstance.CompleteRequest();
}