我在我的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;
}
}
}
我为找到这个问题的解决方案而咨询的帖子是this one、this和this one。我偶然发现了this too,它似乎是有关联的,但没有运气。this last one也很有用,但是我不知道该安装哪种类型的包。
感谢您提供有关如何解决此问题的指导。
发布于 2020-02-26 09:12:57
根据您显示的代码,您正在尝试使用.NET核心中确实不存在的System.Web.HttpContext.Current.Server.MapPath
。
在ASP.NET核心中没有更多的HttpContext静态可用,与System.Web完全一样。
要替换"Server.MapPath",您可以在这里遵循一些指导:https://www.mikesdotnetting.com/Article/302/server-mappath-equivalent-in-asp-net-core
基本上,您需要访问ASP.NET核心将愉快地注入的IHostingEnvironment env
对象。
我建议不要使用静态方法来利用在控制器构造函数中自动执行的构造函数依赖注入。
否则,您也可以调用依赖关系服务来获取实例(有关如何使用依赖关系服务的所有细节在这里有点超出范围,但如果不清楚,请随时发表评论)
从这里,您应该能够获得服务器的路径:
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;
// ...
}
}
另请参阅以下相关问答:How to use IHostingEnvironment
https://stackoverflow.com/questions/60397899
复制相似问题