在我最新的NetCore(2.0)项目中,我使用的是Rotativa.AspNetCore,自从将其发布到SmarterAsp以来,一切工作正常(本地)。
主要问题是,当我尝试通过api生成PDF文件时,该操作无效,并返回错误:
找不到请求的页面。 [404]
XML解析错误:找不到根元素
位置:https://.../api/file/MembershipRegistrationApplication
第1行,第1列:
错误图片:
https://i.stack.imgur.com/J5WAg.jpg
这是我的代码
[HttpPost]
[AllowAnonymous]
[Route("/api/file/MembershipRegistrationApplication")]
public async Task<byte[]> MembershipRegistrationApplication([FromBody] RegisterViewModel model)
{
var education = new List<Education>();
education.Add(_educationService.IncludeEducation(model.Education));
var user = new ApplicationUser
{
Email = model.Email,
PhoneNumber = model.PhoneNumber,
BirthDate = model.BirthDate,
Address = model.Address,
PlaceOfBirth = model.PlaceOfBirth,
MunicipalityOfBirthId = model.MunicipalityId,
IsMale = model.IsMale,
FathersName = model.FathersName,
LastName = model.LastName,
MaritalStatusId = model.MaritalStatusId,
CitizenshipId = model.CitizenshipId,
MunicipalityId = model.MunicipalityId,
FirstName = model.FirstName,
NationalityId = model.NationalityId,
PersonalNumber = model.PersonalNumber,
ForeignMunicipalityOfBirth = model.ForeignMunicipalityOfBirth,
Educations = education,
MaidenName = model.MaidenName
};
user = _accountService.IncludeApplicationUser(user);
return await _pdfGenerator.GenerateMembershipApplication(user);
}
GenerateMembershipApplication方法代码
public async Task<byte[]> GenerateMembershipApplication(ApplicationUser user)
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
var bytePDF = new Rotativa.AspNetCore.ViewAsPdf("File/MembershipApplication", user)
{
PageMargins = { Top = 10, Right = 1, Bottom = 1, Left = 20 },
FileName = string.Concat("MembershipApplication", ".pdf")
}.BuildFile(actionContext);
return await bytePDF;
}
和ajax调用
$.ajax({
url: "/api/file/MembershipRegistrationApplication",
type: 'post',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(model),
success: function (data) {
var pdfAsDataUri = "data:application/pdf;base64,"+data;
window.open(pdfAsDataUri);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg);
}
});
首先,我认为问题出在ajax调用发布的数据上(我也尝试过使用formData,但仍然是相同的错误),并且在尝试了数小时之后,我才发现发生错误的原因是Rotativa。我认为Rotativa引起此问题的另一个原因是,与pdf生成过程无关的其他API可以完美地工作。
我的Rotativa文件夹也有必要的文件,
有人对解决这个问题有什么想法或建议吗?