![](/img/trans.png)
[英]What is the difference between Host and WebHost class in asp.net core
[英]What is the difference between File(), PhysicalFile(), PhysicalFileResult() in ASP.NET Core?
我正在嘗試使用 ASP.NET core 3.1 構建一個 Web API 端點,這將允許應用程序向我發送一個 id,並且響應將與相應的文件一起發送。
這是我的方法
[HttpGet("get")]
public IActionResult Get(Guid id)
{
FoundFileInfo file = PathFinder.Get(id);
if(file == null || !System.IO.File.Exists(file.Fullname))
{
return NotFound();
}
return File(file.Fullname, "image/jpeg");
}
使用相同的代碼,我可以返回File(file.VirtualName, "image/jpeg")
、 new PhysicalFileResult(filename, "image/jpeg")
或PhysicalFile(filename, "image/jpeg")
。 但是它們之間有什么區別,每個的正確用例是什么?
我的最終目標是允許消費者根據端點的響應構造一個IFileInfo實例。 不知何故,我想為消費者提供足夠的信息,如LastModified
、 Length
、 Name
、 PhysicalPath
。 哪種方法適合我的案例?
.NET Core 的File
僅處理虛擬路徑(相對於您的網站)。 PhysicalFile
處理物理(絕對)文件路徑。
PhysicalFile
只是一個返回PhysicalFileResult
的外觀。 但是您可以使用new PhysicalFileResult
手動返回它。
我認為沒有其他區別,選擇主要取決於您如何獲得要返回的文件的位置。 如果您同時擁有虛擬位置和物理位置,則可以選擇兩者中的任何一個( File
或PhysicalFile
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.