Handling FileContentResult when file is not found

前端 未结 2 2002
别跟我提以往
别跟我提以往 2020-12-31 10:51

I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks somet

2条回答
  •  青春惊慌失措
    2020-12-31 11:15

    In ASP.NET Core, use NotFound()

    Your controller must inherit of Controller and the method must return ActionResult

    Example:

    public ActionResult GetFile(string path)
    {
        if (!File.Exists(path))
        {
            return NotFound();
        }
        return new FileContentResult(File.ReadAllBytes(path), "application/octet-stream");
    }
    

提交回复
热议问题