如何解决数据未达到API控制器请求失败
我有一个应用程序,其中UI由Angular js构建,并在Asp.net mvc上后端。在按钮上单击我正在调用角度服务,该服务将json对象传递给asp.net API控制器,而不是普通的mvc控制器。这是因为API控制器必须生成excel文件并返回HttpResponseMessage。现在,当调用从角度服务转到API控制器时,数据尚未到达,并显示为失败请求。在我的路由配置中,我将URL定义为“ {controller} / {action} / {id}”。也可以正常调用常规控制器。我是否需要为api控制器添加网址,以便服务调用到达api控制器。普通的mvc控制器和api控制器也位于同一文件夹中。此外,有关信息,我还在本地开发人员的IIS上以各种设置在https中运行应用程序。以下是用于角度服务和mvc api控制器的代码。
Service:
ServiceSlarDetails.prototype.GetSlarExcelFile = function () {
var link,params;
//link = baseUrl + '/SLAR/GetSlarExcelFile';
link = baseUrl + '/File/GetSlarExcelFile';
params = "'" + JSON.stringify(this.object) + "'";
return $http({
headers: { 'Content-Type': 'application/json','Accept': 'application/json' },method: 'POST',url: link,responseType: "arraybuffer",data: params
}).then(function onSuccess(response) {
return response.data;
}).catch(function onError(data,status,headers,config) {
return NewAlert.show('Oops!','Sorry,something went wrong. Please try again.','error');
});
};
API CONTROLLER:
public class FileController : ApiController
{
[HttpPost]
public HttpResponseMessage GetSlarExcelFile([FromBody] string ExcelTable)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
object obj = null;
HttpResponseMessage FailReturn = new HttpResponseMessage(HttpStatusCode.NoContent);
try
{
var data = (DataTable)JsonConvert.DeserializeObject(ExcelTable,(typeof(DataTable)));
data.TableName = "Slar";
if (data.Rows.Count > 0)
{
//string path = HttpContext.Current.Server.MapPath("~/ExcelFiles");
//if (!Directory.Exists(path))
//{
// Directory.CreateDirectory(path);
//}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(data);
MemoryStream ms = new MemoryStream();
{
//wb.SaveAs(path + "/" + DateTime.Now.Ticks + ".xlsx");
wb.SaveAs(ms);
result.Content = new ByteArrayContent(ms.GetBuffer());
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "Invoices.xlsx";
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
result.Content.Headers.ContentLength = ms.Length;
ms.Seek(0,SeekOrigin.Begin);
}
}
}
// obj = new { resp = data,error = "" };
}
catch (Exception ex)
{
//Logger.Error(ex.Message);
//obj = new { resp = "",error = ex.Message.ToString() };
}
return result;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。