.Net Web Api - ArrayBuffer搞砸了其他api调用

时间:2017-10-11 04:09:00

标签: .net http asp.net-web-api response httpresponsemessage

我有几个web api控制器函数,如下所示

[HttpGet]
    [Route("GetCycle")]
    public HttpResponseMessage GetCycle(string type) {
        try
        {
            Cycle oCycleClass = Data.GetCycle(type);
            return Request.CreateResponse(oCycleClass);

        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, ex.Message);
        }

    }

这是另一个按预期工作的控制器。

[HttpPost]
    [Route("GetDataDownload")]
    public HttpResponseMessage GetDataExport([FromBody]DataObject objectData)
    {
        try
        {
            byte[] excelData = Data.GetDataExport(objectData);

            String timeStamp = DateTime.Now.ToString("yyyy-MM-dd HH mm ss");

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(excelData);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = "DataExport - " + focalData.User.ID + " - " + timeStamp + ".xlsx";

            return response;
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, ex.Message);
        }
    }

GetDataExport功能之后执行任何其他控制器后出现错误。看起来HttpResponseMessage有点被破坏了。

例如:

1。执行GetCycle - 结果:成功

2。执行GetDataExport - 结果:成功

3。执行GetCycle - 结果:失败

GetDataExport功能

public static byte[] GetDataExport(DataObject dataObject)
    {
        DataTable view = Common.ConvertToDataTable<View>(dataObject.Views);
        string filter = Common.ConvertFiltersToSQLStatement(dataObject.Filters);

        DataSet ds;

        if (dataObject.DownloadWithFilters)
        {
            ds = InterfaceDataClass.GetEmployees(dataObject.CycleId, view, filter, 1, 0, true);
        }
        else
        {
            ds = InterfaceDataClass.GetEmployees(dataObject.CycleId, view, string.Empty, 1, 0, true);
        }

        using (ExcelPackage pck = new ExcelPackage())
        {
            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Employees");

            //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
            ws.Cells["A1"].LoadFromDataTable(ds.Tables[0], true);

            //Format the header column
            using (ExcelRange rng = ws.Cells["A1:BB1"])
            {
                rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
                rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(122, 122, 122));
                rng.Style.Font.Color.SetColor(Color.WhiteSmoke);
            }

            return pck.GetAsByteArray();
        }
    }

ExcelPackage来自名为EPPlus

的库

1 个答案:

答案 0 :(得分:0)

API完全没有错。问题在于如何调用和执行API。