C#:如何安装System.Web
我正在我的C#:如何安装System.Web,c#,visual-studio-code,nuget-package,C#,Visual Studio Code,Nuget Package,我正在我的ubuntu18.04上使用visualstudio代码version1.42。我刚刚通过终端成功安装了sudo dotnet add包Google.api.Drive.v3,但我找不到在我的C项目上安装System.Web 我尝试了很多不同的方法: 1) sudo dotnet添加包Microsoft.AspNet.WebApi 2) sudo dotnet添加包Microsoft.AspNet.Mvc-版本5.2.7 3) sudo dotnet添加包Microsoft.AspN
ubuntu18.04
上使用visualstudio代码version1.42
。我刚刚通过终端成功安装了sudo dotnet add包Google.api.Drive.v3
,但我找不到在我的C
项目上安装System.Web
我尝试了很多不同的方法:
1) sudo dotnet添加包Microsoft.AspNet.WebApi
2) sudo dotnet添加包Microsoft.AspNet.Mvc-版本5.2.7
3) sudo dotnet添加包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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统线程;
//使用System.Web;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.Mvc;
命名空间IHostingEnvironmentExample.Controllers
{
公共类HomeController:控制器
{
私人IHostingEnvironment;
公共家庭控制器(IHostingEnvironment环境)
{
_env=env;
}
公共IActionResult索引()
{
var webRoot=_env.WebRootPath;
var file=System.IO.Path.Combine(webRoot,“test.txt”);
System.IO.File.WriteAllText(文件“Hello World!”);
返回视图();
}
}
}
名称空间WebApi2.Models
{
公共类GoogleDriveFileRepository
{
//定义的范围。
公共静态字符串[]范围={DriveService.Scope.Drive};
//行动。。。。
//创建驱动器API服务。
公共静态驱动器服务GetService()
{
//行动。。。。
公共静态列表GetDriveFiles()
{
//其他行动。。。。
}
//文件上传到谷歌硬盘。
公共静态无效文件上载(HttpPostedFileBase文件)
{
如果(file!=null&&file.ContentLength>0)
{
DriveService=GetService();
string path=path.Combine(HttpContext.Current.Server.MapPath(“~/GoogleDriveFiles”),
GetFileName(file.FileName));
file.SaveAs(路径);
var FileMetaData=new Google.api.Drive.v3.Data.File();
FileMetaData.Name=Path.GetFileName(file.FileName);
FileMetaData.MimeType=MimeMapping.GetMimeMapping(路径);
FileResource.CreateMediaUpload请求;
使用(var stream=new System.IO.FileStream(path,System.IO.FileMode.Open))
{
request=service.Files.Create(FileMetaData、stream、FileMetaData.MimeType);
request.Fields=“id”;
request.Upload();
}
}
}
//通过文件ID从Google Drive下载文件。
公共静态字符串下载GoogleFile(字符串文件ID)
{
DriveService=GetService();
字符串FolderPath=System.Web.HttpContext.Current.Server.MapPath(“/GoogleDriveFiles/”);
fileresource.GetRequest request=service.Files.Get(fileId);
字符串文件名=request.Execute().Name;
字符串FilePath=System.IO.Path.Combine(FolderPath,FileName);
MemoryStream stream1=新的MemoryStream();
request.MediaDownloader.ProgressChanged+=(Google.api.Download.IDownloadProgress)=>
{
开关(进度状态)
{
案例下载状态。正在下载:
{
Console.WriteLine(progress.bytes下载);
打破
}
案例下载状态。已完成:
{
控制台.WriteLine(“下载完成”);
存储流(stream1,FilePath);
打破
}
案例下载状态。失败:
{
Console.WriteLine(“下载失败”);
打破
}
}
};
请求下载(stream1);
返回文件路径;
}
}
}
我为找到解决这个问题的方法而咨询的帖子也是。
我遇到了似乎有关联但没有运气的事情。
也很有用,但是我不知道应该安装哪种类型的软件包
感谢您提供有关如何解决此问题的指导。根据您显示的代码,您正在尝试使用System.Web.HttpContext.Current.Server.MapPath
,它在.NET Core中确实不存在
ASP.NET核心中不再有可用的HttpContext静态文件,整个System.Web也不再有可用的HttpContext静态文件
要替换“Server.MapPath”,您可以按照以下指南操作:
基本上,您需要访问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;
// ...
}
}
另请参阅此相关问答:您正在进行.NET核心开发,因此System.Web
超出了范围。您需要从中获得什么?没有您提到或链接的软件包(Microsoft.AspNet.WebApi、Microsoft.AspNet.Mvc、System.Web.Http、System.Web.Providers)与System.Web相关。正如您提到的Ubuntu,我想您有.NET Core项目。但是,System.Web是一个.NET Framework程序集。为什么需要System.Web?将其删除并使用.NET Core程序集中的类。如果您需要Web客户端,则应使用System.Http.NET(与HttpCli一起使用)