2015-07-08 18 views
17

tôi nhận được ngoại lệ này không liên tục trong asp.net MVC 5 c ứng dụng # web của tôi:Server không thể thêm tiêu đề sau khi tiêu đề HTTP đã được gửi tải lên file

Server cannot append header after HTTP headers have been sent.

Nó chỉ xảy ra tải lên hình ảnh để phương pháp S3 (điều khiển Api Web).

Các presendrequestheaders trong Global.asax

protected void Application_PreSendRequestHeaders(object sender, EventArgs e) 
     { 
      HttpApplication app = sender as HttpApplication; 
      if (app != null && 
       app.Context != null) 
      { 
       app.Context.Response.Headers.Remove("Server"); 
      } 
     } 

Phương pháp kích hoạt các lỗi:

[HttpPost] 
    [Route("UploadImageJSON")] 
    public IHttpActionResult UploadImageJSON(HttpRequestMessage request) 
    { 
     var httpRequest = HttpContext.Current.Request; 

     // Check if files are available 
     if (httpRequest.Files.Count != 1) return BadRequest(); 

     var postedFile = httpRequest.Files[0]; 

     var contentType = postedFile.ContentType; 
     if (!contentType.Contains("image")) 
     { 
      return StatusCode(HttpStatusCode.NotAcceptable); 
     } 




     var keyUploadFiles = Constants.UrlS3Amazon + 
          S3.UploadToS3WithStream(postedFile.InputStream, contentType); 


     return Json(JsonConvert.SerializeObject(keyUploadFiles)); 


    } 

EDIT: thêm thông tin ... Tôi đã App web của tôi được tổ chức trong Elastic cây đậu với một cân bằng tải, cân bằng tải đã cài đặt một chứng chỉ SSL, và kết nối giữa cân bằng tải và các trường hợp EC2 là ở cổng 80. Có lẽ nó có thể hữu ích.

Nhật ký ELMAH:

System.Web.HttpException (0x80004005): Server cannot append header after HTTP headers have been sent. at System.Web.HttpHeaderCollection.SetHeader(String name, String value, Boolean replace) at Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders.Set(String key, String[] values) at Microsoft.Owin.Infrastructure.OwinHelpers.AppendHeaderUnmodified(IDictionary 2 headers, String key, String[] values) at Microsoft.Owin.ResponseCookieCollection.Append(String key, String value, CookieOptions options) at Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler.<ApplyResponseGrantAsync>d__b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<ApplyResponseCoreAsync>d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<TeardownAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware 1.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware 2.<Invoke>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware 2.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Mapping.MapMiddleware.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Cảm ơn !!

+0

Nó đến mỗi khi bạn tải lên hình ảnh bằng phương pháp của mình? Bởi vì bạn đã nói lúc đầu đó là một vấn đề liên tục ... – D4rkTiger

+0

Bạn có bất kỳ mô-đun http nào mà phản hồi đi qua không? – hivie7510

+0

Nop mà tôi biết – chemitaxis

Trả lời

1

bạn đã thử xóa ứng dụng.Context.Response.Headers.Remove ("Máy chủ"); tôi nghĩ rằng đây là vấn đề?

Các vấn đề liên quan