Server cannot set content type after HTTP headers

2019-08-12 15:12发布

I need to render a pdf while clicking a button.

i am using the following js code:

function RenderPDf() {
            //for
            window.open("/Resources/RenderPDF", "", 'toolbar=no,titlebar=no, directories=no, location=no,status=yes, menubar=no, resizable=yes, scrollbars=yes,width=1010, height=680,left=0,top=0');
            return false;
        }

But i am getting the following error sometimes:

Message :Server cannot set content type after HTTP headers have been sent.
Source :System.Web
Stack Trace :   at System.Web.HttpResponse.set_ContentType(String value)
  at System.Web.UI.Page.SetIntrinsics(HttpContext context, Boolean allowAsync)
  at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  at ASP.views_file_viewtaxreturn_aspx.ProcessRequest(HttpContext context)
  at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
  at System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page)
  at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)

  at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()

  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)

  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  at System.Web.Mvc.Controller.ExecuteCore()
  at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
  at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

TargetSite :Void set_ContentType(System.String)

Please suggest with this exception.

3条回答
神经病院院长
2楼-- · 2019-08-12 15:39

Solved this error by returning empty result in the controller

return new EmptyResult();

查看更多
forever°为你锁心
3楼-- · 2019-08-12 15:40

Anything, and I mean anything sent to the client before the headers you intend to set can cause this error. Somewhere, under some conditions, your app is sending something to the client before your desired headers.

查看更多
Deceive 欺骗
4楼-- · 2019-08-12 15:59

As a suggestion, when I've had this problem it's been because I've had whitespace / new lines / html code before I started my code blocks. Check you're not sending something to the client before you call your function.

查看更多
登录 后发表回答