我有几个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
答案 0 :(得分:0)
API完全没有错。问题在于如何调用和执行API。