Server cannot set content type after sending HTTP headers

I need to make a PDF by 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 sometimes I get the following error:

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 offer with this exception.

+1
source share
3 answers

This error is resolved by returning an empty result to the controller

return new EmptyResult ();

+6
source

As a suggestion, when I had this problem, it was because I had the code whitespace / new lines / html before I started my code blocks. Make sure you do not send something to the client before you call your function.

0
source

That's all, and I mean anything sent to the client before the headers you intend to set can cause this error. Somewhere in some conditions, your application sends something to the client in front of your desired headers.

0
source

All Articles