【问题标题】:404 Error when testing REST API returning File测试 REST API 返回文件时出现 404 错误
【发布时间】:2021-09-08 21:23:16
【问题描述】:

我使用 .NET Core 3.1 创建了一个 REST Get API,以便在对 MS Word 模板进行一些更改后返回一个文件以供下载。如果我在 Visual Studio 中运行 API 并在 Postman 中测试这个 API,那么它工作正常,但如果我在 IIS 中托管这个 API,那么我会收到 404 错误。在日志中它说 - “System.InvalidOperationException:无法从返回类型为'Microsoft.AspNetCore.Mvc.FileResult'的操作方法返回null。” 我创建了另外 2 个返回简单消息的 API(GET 和 POST)。两者都在 IIS 中工作。

代码:

[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;
        }
    }

对于这种类型的 REST API,是否需要在 IIS 中进行任何设置?

【问题讨论】:

  • 所以框架说不要返回 null。那你为什么要返回null呢?您也在丢弃您的异常详细信息。
  • 这里我在异常中返回 null,但在实际代码中,我将附加到日志文件中。日志文件中没有写入任何内容。它缝合了代码在 catch 部分没有到达的地方
  • 为什么你认为它没有到达 catch 部分?你从那里返回 null 。这是您返回 null 的唯一地方。并且错误消息专门抱怨您的操作方法返回 null。所以答案是当返回类型为 FileResult 时不返回 null。您必须更改方法以允许不同的结果类型,或者停止返回 null 并再次 throw; 异常。
  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…

标签: .net asp.net-core


【解决方案1】:

// 准备文件路径
string fullFilePath = "ABC.docx";

我认为这个代码是错误的原因。


可以通过修改fullFilePath的路径为实际物理路径来测试。比如格式是

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

然后测试。


你也可以记录下路径,然后根据路径去找文件,如果没有,应该会出现404。


建议:

你可以阅读下面的答案,它对你有用。

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

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 2015-04-10
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多