C#-发送HTTP头后,服务器无法设置内容类型

C#-发送HTTP头后,服务器无法设置内容类型,c#,model-view-controller,reporting-services,C#,Model View Controller,Reporting Services,我试图从MVC应用程序下载excel格式的SSRS报告 Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.ms-excel"; Response.AppendHeader("C

我试图从MVC应用程序下载excel格式的SSRS报告

    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + reportName + "." + extension);
    Response.BinaryWrite(result);
    Response.Flush();
    Response.End();
但我得到的错误是-

System.Web.HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.
我尝试将Response.Buffer更改为BufferOutput,但仍然得到相同的错误。
我在这里遗漏了什么?

在下面的代码中,我试图下载一个excel文件作为-

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader($"content-disposition", "attachment;filename=" + "{reportFileName}" + ".xlsx");
        using (System.IO.MemoryStream MyMemoryStream = new MemoryStream())
        {
            MyMemoryStream.Write(result, 0 , result.Length);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();

            return File(MyMemoryStream.ToArray(), "application/octet-stream");
        }