Net Core api 中获取应用程序物理路径wwwroot

14 篇文章 1 订阅

如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径,如下所示:

// Classic ASP.NET

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string physicalWebRootPath = Server.MapPath("~/");
        
        return Content(physicalWebRootPath);
    }
}


但是在ASPNET Core中不存在Server.MapPath()方法,Controller基类也没有Server属性。

 

 

在Asp.Net Core中取得物理路径:

从ASP.NET Core RC2开始,可以通过注入 IHostingEnvironment 服务对象来取得Web根目录和内容根目录的物理路径,如下所示:

 

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCorePathMapping
{
    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);
        }
    }
}


 

我在 ~/Code/AspNetCorePathMapping 目录下创建了一个示例 Asp.Net Core 应用程序,当我运行时,控制器将返回以下两个路径:

这里要注意区分Web根目录 和 内容根目录的区别:

Web根目录是指提供静态内容的根目录,即asp.net core应用程序根目录下的wwwroot目录

内容根目录是指应用程序的根目录,即asp.net core应用的应用程序根目录

 

ASP.NET Core RC1

在ASP.NET Core RC2之前 (就是ASP.NET Core RC1或更低版本),通过 IApplicationEnvironment.ApplicationBasePath 来获取 Asp.Net Core应用程序的根目录(物理路径) :

using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.PlatformAbstractions;

namespace AspNetCorePathMapping
{
    public class HomeController : Controller
    {
        private readonly IApplicationEnvironment _appEnvironment;

        public HomeController(IApplicationEnvironment appEnvironment)
        {
            _appEnvironment = appEnvironment;
        }

        public ActionResult Index()
        {
            return Content(_appEnvironment.ApplicationBasePath);
        }
    }
}

 

 

 

 

 

 

.net core保存图片,路径问题

0

 悬赏园豆:10 [已解决问题] 浏览: 1164次 解决于 2018-11-14 18:05 

我有一个保存上传图片的.net core部署在我的centos7服务器上,但是每次图片并没有保存到站点的/home/wwwroot/file/wwwroot/images/文件夹下,而是在/home/wwwroot/文件夹,很神奇,filepath的路径可以输出/home/wwwroot/file/wwwroot/images/,但是不知道为什么就是存不到那里去。
这个我的源码:

这是我用来测试图片路径的接口:http://static.lovemoqing.com/Home/Test/
可以输出/home/wwwroot/file/wwwroot/images/

希望可以帮我解答一下 。我对文件夹操作不是很熟悉,如果要设置用户组权限啥的希望可以贴一下命令(/ω\)不胜感激

 

最佳答案

1

bmp2.Save 传参错了,应该是 filePath ,你写成了 filename

 

.net core 获取网站根目录

string rootdir = AppContext.BaseDirectory;
DirectoryInfo Dir = Directory.GetParent(rootdir);
string root = Dir.Parent.Parent.FullName;

 

 

 

.Net Core 获取程序当前路径
测试环境
测试代码
输出
win-x64
linux-64
总结
测试环境
主要对.Net Core下的几种获取文件方式进行测试,
测试环境
.Net Standard 2.0 类库 ClassLibrary1
.Net Core 3.1控制台程序 LinuxPathTest

发布分别打包为linux-x64和win-x64 无依赖 单文件 裁剪未使用程序集

测试代码
ClassLibrary1
 

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace ClassLibrary1
{
    public class Library: IEnumerable<string>
    {
        public IEnumerator<string> GetEnumerator()
        {
            yield return $"{Environment.CurrentDirectory}";
            yield return $"{Directory.GetCurrentDirectory()}";
            yield return $"{GetType().Assembly.Location}";
            yield return $"{Process.GetCurrentProcess().MainModule.FileName}";
            yield return $"{AppDomain.CurrentDomain.BaseDirectory}";
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

LinuxPathTest

using ClassLibrary1;

using System;
using System.Diagnostics;
using System.IO;

namespace LinuxPathTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-------------------------------");
            foreach (var item in new Library())
            {
                Console.WriteLine($"{item}");
            }
            Console.WriteLine("-------------------------------");
            Console.WriteLine($"{Environment.CurrentDirectory}");
            Console.WriteLine($"{Directory.GetCurrentDirectory()}");
            Console.WriteLine($"{(new object()).GetType().Assembly.Location}");
            Console.WriteLine($"{Process.GetCurrentProcess().MainModule.FileName}");
            Console.WriteLine($"{AppDomain.CurrentDomain.BaseDirectory}");
            Console.WriteLine($"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}"); // .net standard 不支持
            Console.ReadLine();
        }
    }
}

输出

win-x64

可执行文件所在路径 E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish

-------------------------------
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\ClassLibrary1.dll
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish\LinuxPathTest.exe
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\
-------------------------------
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\System.Private.CoreLib.dll
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish\LinuxPathTest.exe
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\

linux-64

可执行文件路径 /home/用户名/Desktop/Test

-------------------------------
/home/用户名/Desktop/Test
/home/用户名/Desktop/Test
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/ClassLibrary1.dll
/home/用户名/Desktop/Test/LinuxPathTest
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/
-------------------------------
/home/用户名/Desktop/Test
/home/用户名/Desktop/Test
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/System.Private.CoreLib.dll
/home/用户名/Desktop/Test/LinuxPathTest
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/

总结

.Net Core 打包为自宿主无依赖的单文件之后,因为多出一步将文件释放到临时文件的操作,
一些获取文件路径的方式得到的结果可能和.Net Framework 下不一致。

 

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值