-1

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • So the framework says don't return null. Why are you returning null then? You're throwing away your exception details too. – mason Sep 08 '21 at 21:28
  • Here i return null in exception but in real code i am appending into a log file. And nothing is written in the log file. It seams the code does not reach in catch section – Ajay Pal Singh Sep 08 '21 at 21:49
  • Why do you think that it doesn't reach the catch section? You return null from there. That's the only place you return null from. And the error message is specifically complaining about your action method returning null. So the answer is don't return null when the return type is FileResult. You must either change the method to allow different result types, or stop returning null and instead `throw;` the exception again. – mason Sep 08 '21 at 22:17
  • If my reply is helpful, please accept it as answer(click on the mark option beside the reply to toggle it from greyed out to fill in.), see https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jason Pan Sep 10 '21 at 07:03

1 Answers1

0

// Prepare file path
string fullFilePath = "ABC.docx";

I think this code is the cause of the error.


You can test by modifying the path of fullFilePath to the actual physical path. For example, the format is

string fullFilePath = @"D:\myproject\templatefile\ABC.docx";

Then test.


You also can log the path, then go to find the file according to the path, if not, then it should appear 404.


Suggestion:

You can read below answer, it useful to you.

What is the equivalent of Server.MapPath in ASP.NET Core?

Jason Pan
  • 15,263
  • 1
  • 14
  • 29