首页 > 解决方案 > C#:如何安装 System.Web

问题描述

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

网络

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

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

我为找到解决此问题的方法而咨询的帖子是这个这个,还有这个。我也遇到了这个,这似乎是相关的,但没有运气。最后一个也很有用,但是我对要安装哪种类型的软件包感到困惑。

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

标签: c#visual-studio-codenuget-package

解决方案


根据您显示的代码,您正在尝试使用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 Core 会很高兴地注入该对象。

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

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

由此,您应该能够获得服务器的路径:

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;


        // ...
    }
}

另请参阅此相关问答:如何使用 IHostingEnvironment


推荐阅读