ASP.NET Core中Server.MapPath的等价物是什么?

 2023-02-17    319  

问题描述

我在一些代码中有这条线我想复制到我的控制器中,但编译器抱怨

当前上下文中不存在名称’服务器’

ASP.NET Core中Server.MapPath的等价物是什么?

var UploadPath = Server.MapPath("~/App_Data/uploads")

如何在ASP.NET核心中达到等同物?

推荐答案

更新:弃用ihostingenvironment.请参阅下面的更新.

在ASP.NET核心2.2及更低中,托管环境已抽象使用界面, ihostingenvironment

ContentRootPath 属性将为您访问应用程序内容文件的绝对路径.

您还可以使用该属性, WebRootPath 如果要访问Web可乘坐的Root Path(默认情况下的WWW文件夹)

您可以将此依赖注入控制器中并按如下方式访问:

public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(webRootPath + "\n" + contentRootPath);
        }
    }

更新 – .NET Core 3.0及更高版本

ihostingenvironment已被标记为obsolete与.NET Core 3.0已指出,由@ amir133 .您应该使用 iwebhostenvironment 而不是 ihostingenvironment .请参阅它下面.

Microsoft在这些接口之间整齐地分离了主机环境属性.请参阅下面的界面定义:

namespace Microsoft.Extensions.Hosting
{
  public interface IHostEnvironment
  {
    string EnvironmentName { get; set; }
    string ApplicationName { get; set; }
    string ContentRootPath { get; set; }
    IFileProvider ContentRootFileProvider { get; set; }
  }
}

namespace Microsoft.AspNetCore.Hosting
{
  public interface IWebHostEnvironment : IHostEnvironment
  {
    string WebRootPath { get; set; }
    IFileProvider WebRootFileProvider { get; set; }
  }
}

其他推荐答案

.NET 6(.NETCORE 3及更高版本)

例如,我想找到~/wwwroot/CSS

public class YourController : Controller
{
    private readonly IWebHostEnvironment _webHostEnvironment;

    public YourController (IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment= webHostEnvironment;
    }

    public IActionResult Index()
    {
        string webRootPath = _webHostEnvironment.WebRootPath;
        string contentRootPath = _webHostEnvironment.ContentRootPath;

        string path ="";
        path = Path.Combine(webRootPath , "CSS");
        //or path = Path.Combine(contentRootPath , "wwwroot" ,"CSS" );
        return View();
    }
}

一些技巧

此外,如果您没有控制器或服务,请按照最后一部分跟踪并将其作为单例作为单例注册.
然后,在startup.configureservices:

services.AddSingleton<your_class_Name>();

最后,注入your_class_Name您需要它.


.NET Core 2

例如,我想找到~/wwwroot/CSS

public class YourController : Controller
{
    private readonly IHostingEnvironment _HostEnvironment; //diference is here : IHostingEnvironment  vs I*Web*HostEnvironment 

    public YourController (IHostingEnvironment HostEnvironment)
    {
        _HostEnvironment= HostEnvironment;
    }

    public ActionResult Index()
    {
        string webRootPath = _HostEnvironment.WebRootPath;
        string contentRootPath = _HostEnvironment.ContentRootPath;

        string path ="";
        path = Path.Combine(webRootPath , "CSS");
        //or path = Path.Combine(contentRootPath , "wwwroot" ,"CSS" );
        return View();
    }
}

moredetails

感谢@Ashin但是IHostingEnvironment在MVC核心3中已经过量!!

根据这个:

过时类型(警告):

Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName

新类型:

Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments

所以你必须使用IWebHostEnvironment而不是IHostingEnvironment.

其他推荐答案

在大多数情况下,已接受的答案的建议是足够的,因为它取决于依赖注入 – 仅限于控制器和视图:为了有一个正确的Server.MapPath更换可以从非单例辅助类别访问,我们可以在App’s Startup.cs文件的Configure()方法的末尾添加以下行的代码:

// setup app's root folders
AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
AppDomain.CurrentDomain.SetData("WebRootPath", env.WebRootPath);

这样,我们将能够以下列方式从任何类(包括但不限于控制器和视图)中检索它们:

var contentRootPath = (string)AppDomain.CurrentDomain.GetData("ContentRootPath");
var webRootPath = (string)AppDomain.CurrentDomain.GetData("WebRootPath");

可以进一步利用它来创建一个静态辅助方法,允许我们具有与良好旧的功能相同的功能:

public static class MyServer 
{
    public static string MapPath(string path)
    {
        return Path.Combine(
            (string)AppDomain.CurrentDomain.GetData("ContentRootPath"), 
            path);
    }
}

可以通过以下方式使用:

var docPath = MyServer.MapPath("App_Data/docs");

有关此方法的其他信息和一些背景,请查看这篇文章在我的博客上.

以上所述是小编给大家介绍的ASP.NET Core中Server.MapPath的等价物是什么?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:http://www.77isp.com/post/34268.html

=========================================

http://www.77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。