I have created a REST Get API using .NET Core 3.1 to return a file for download after making some changes in the MS Word Template. If I run the API in Visual Studio and test this API in Postman then it works fine but if i host this API in IIS then I am getting 404 error. In the log it says - "System.InvalidOperationException: Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.FileResult'." I have created 2 more APIs (GET & POST) returning simple message. Both are working in IIS.
Code :
[HttpGet("FM/CreateWordTemplate/{FromDate}/{ToDate}")]
public FileResult CreateWordTemplate(string FDate, string TDate)
{
try
{
DateTime FromDate = Convert.ToDateTime(FDate);
DateTime ToDate = Convert.ToDateTime(TDate);
// Prepare file path
string fullFilePath = "ABC.docx";
// Copy file content to MemeoryStream via byte array
MemoryStream stream = new MemoryStream();
byte[] fileBytesArray = System.IO.File.ReadAllBytes(fullFilePath);
stream.Write(fileBytesArray, 0, fileBytesArray.Length);
stream.Position = 0;
// Edit word document content
using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true))
{
MainDocumentPart mainDocPart = document.MainDocumentPart;
#region General Document
Run HeaderRun;
// Fetch Bookmark for HeaderMonth and write into Template
BookmarkStart bmStart = clsCommon.findHeaderBookMarkStart(document, "HeaderMonth");
HeaderRun = AddHeaderMonthYear(FromDate, ToDate);
if (bmStart != null)
{
bmStart.Parent.InsertAfter<Run>(HeaderRun, bmStart);
}
// Fetch Bookmark for HeaderDates and write into Template
bmStart = clsCommon.findHeaderBookMarkStart(document, "HeaderDates");
HeaderRun = AddHeaderDates(FromDate, ToDate);
if (bmStart != null)
{
bmStart.Parent.InsertAfter<Run>(HeaderRun, bmStart);
}
#endregion
}
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");
}
catch (Exception ex)
{
return null;
}
}
Is there any settings to be done in IIS for this type of REST API?