C#:如何安装 System.Web

C#: How to install System.Web

我在 Ubuntu 18.04 上使用 Visual Studio Code 版本 1.42。我刚刚通过终端成功安装了 sudo dotnet add package Google.Apis.Drive.v3,但我找不到在我的 C# 项目上安装 System.Web 的方法。

我尝试了很多不同的方法:

1) sudo dotnet add package Microsoft.AspNet.WebApi

2) sudo dotnet add package Microsoft.AspNet.Mvc -Version 5.2.7

3) sudo dotnet add package Microsoft.AspNet.Mvc

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
//using System.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;


namespace IHostingEnvironmentExample.Controllers
{
    public class HomeController : Controller
    {
        private IHostingEnvironment _env;
        public HomeController(IHostingEnvironment env)
        {
            _env = env;
        }
        public IActionResult Index()
        {
            var webRoot = _env.WebRootPath;
            var file = System.IO.Path.Combine(webRoot, "test.txt");
            System.IO.File.WriteAllText(file, "Hello World!");
            return View();
        }
    }
}


namespace WebApi2.Models
{
    public class GoogleDriveFilesRepository 
    {
       //defined scope.
        public static string[] Scopes = { DriveService.Scope.Drive };
        // Operations....

        //create Drive API service.
        public static DriveService GetService()
        {
             //Operations.... 

        public static List<GoogleDriveFiles> GetDriveFiles()
        {
            // Other operations....
        }

       //file Upload to the Google Drive.
        public static void FileUpload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                DriveService service = GetService();

                string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),
                Path.GetFileName(file.FileName));
                file.SaveAs(path);

                var FileMetaData = new Google.Apis.Drive.v3.Data.File();
                FileMetaData.Name = Path.GetFileName(file.FileName);
                FileMetaData.MimeType = MimeMapping.GetMimeMapping(path);

                FilesResource.CreateMediaUpload request;

                using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
                {
                    request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
                    request.Fields = "id";
                    request.Upload();
                }
            }
        }


        //Download file from Google Drive by fileId.
        public static string DownloadGoogleFile(string fileId)
        {
            DriveService service = GetService();

            string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/");
            FilesResource.GetRequest request = service.Files.Get(fileId);

            string FileName = request.Execute().Name;
            string FilePath = System.IO.Path.Combine(FolderPath, FileName);

            MemoryStream stream1 = new MemoryStream();

            request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
            {
                switch (progress.Status)
                {
                    case DownloadStatus.Downloading:
                        {
                            Console.WriteLine(progress.BytesDownloaded);
                            break;
                        }
                    case DownloadStatus.Completed:
                        {
                            Console.WriteLine("Download complete.");
                            SaveStream(stream1, FilePath);
                            break;
                        }
                    case DownloadStatus.Failed:
                        {
                            Console.WriteLine("Download failed.");
                            break;
                        }
                }
            };
            request.Download(stream1);
            return FilePath;
        }
    }
}

Post 我咨询过这个问题的解决方案 this one, this, also this one。 我遇到了 this too 这似乎是相关的但没有运气。 this last one 也很有用,但是我对安装哪种类型的软件包感到困惑。

感谢您提供有关如何解决此问题的指导。

根据您显示的代码,您正在尝试使用 System.Web.HttpContext.Current.Server.MapPath,它在 .NET Core 中确实不存在。

ASP.NET Core 中不再有可用的 HttpContext 静态,System.Web 完全没有。

要替换 "Server.MapPath",您可以按照此处的一些指导进行操作:https://www.mikesdotnetting.com/Article/302/server-mappath-equivalent-in-asp-net-core

基本上,您需要访问一个 IHostingEnvironment env 对象,ASP.NET 核心会很乐意注入。

我建议不要使用静态方法来利用在控制器构造函数中自动执行的构造函数依赖注入。

否则你也可以调用依赖服务来获取实例(关于如何使用依赖服务的所有细节有点超出这里的范围,但如果不清楚,请随时评论)

从这里,你应该可以得到服务器的路径:

public class HomeController : Controller 
{ 
    private IHostingEnvironment _env;
    // Injection of IHostingEnvironment dependency through constructor
    public HomeController(IHostingEnvironment env)
    {
        _env = env;
    }

    public void MyMethod() 
    {
        // here you get your replacement of "Server.MapPath" :
        var serverPath = _env.WebRootPath;


        // ...
    }
}

另请参阅此相关问答: