ASP.NET MVC 当前上下文中不存在名称 'File'

ASP.NET MVC The name 'File' does not exist in the current context

我目前正在我的 MVC WEB Api 应用程序中编写一个简单的控制器方法,该方法从 Dropbox 帐户下载文件。问题是我无法 return 方法中的文件:它说 "The name 'File' does not exist in the current context",但在文档中可以调用此构造函数。
代码:

public async Task<FileResult> GetFile(string folder, string file)
{
    using (var dbx = new DropboxClient("generated token key"))
    {
        using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
        {
            var result = await response.GetContentAsStringAsync();
            return File(result, file);
        }
    }
}

完成DropBoxController.csclass:

using Dropbox.Api;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Mvc;

namespace TestDropBox.Controllers
{
    public class DropBoxController : ApiController
    {
        // GET: api/DropBox
        public async Task<IEnumerable<string>> Get()
        {

            List<string> AccountData= new List<string>();
            using (var dbx = new DropboxClient("generated token"))
            {
                var full = await dbx.Users.GetCurrentAccountAsync();
                Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);

                AccountData.Add(full.Name.DisplayName);
                AccountData.Add(full.Email);

            }

            return DatiAccount;
        }

        [System.Web.Http.HttpGet]
        public async Task<FileResult> GetFile(string folder, string file)
        {
            using (var dbx = new DropboxClient("generated token"))
            {
                using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
                {
                    var result = await response.GetContentAsStringAsync();
                    return new File(result, file);
                }
            }
        }


        // GET: api/DropBox/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/DropBox
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/DropBox/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/DropBox/5
        public void Delete(int id)
        {
        }
    }
}

我该如何解决这个问题?谢谢。

您继承自 ApiController,因此 System.Web.Mvc.File 构造函数不可访问(受保护),这就是您收到错误的原因。

看看这个话题:

您正在使用 ApiController。您更新后的方法可能类似于...

[System.Web.Http.HttpGet]
public async Task<HttpResponseMessage> GetFile(string folder, string fileName) {
    using (var dbx = new DropboxClient("generated token")) {
        using (var file = await dbx.Files.DownloadAsync(folder + "/" + fileName)) {
            var content = await file.GetContentAsStringAsync();
            var statuscode = HttpStatusCode.OK;
            var response = Request.CreateResponse(statuscode);
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(content ?? ""));
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                FileName = fileName
            };
            return response;
        }
    }
}