ContentRoot:  C:\MyApp\wwwroot
WebRoot:      C:\MyApp\wwwroot\wwwroot

默认情况下 contentRoot是使用程序更目录

可以查看源代码Microsoft.AspNetCore.Hosting.WebHostBuilder 248行

 var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory);  private string ResolveContentRootPath(string contentRootPath, string basePath) 334行{if (string.IsNullOrEmpty(contentRootPath)){return basePath;}if (Path.IsPathRooted(contentRootPath)){return contentRootPath;}return Path.Combine(Path.GetFullPath(basePath), contentRootPath);}

我们可以清楚的看到contentRootPath 就是默认程序目录 由此我们可以更改默认目录到我们物流硬盘的任何位置作为静态分布式文件的存放目录,大型的系统对于静态文件都是分开的 所以这个很好的解决了我们的物理存放文件路径的问题

在来webroot

_hostingEnvironment.Initialize(contentRootPath, _options); 251行

Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions  扩张方法就是合并2个文件路径

 public static void Initialize(this IHostingEnvironment hostingEnvironment, string contentRootPath, WebHostOptions options){if (options == null){throw new ArgumentNullException(nameof(options));}if (string.IsNullOrEmpty(contentRootPath)){throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));}if (!Directory.Exists(contentRootPath)){throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath));}hostingEnvironment.ApplicationName = options.ApplicationName;hostingEnvironment.ContentRootPath = contentRootPath;hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath);var webRoot = options.WebRoot;if (webRoot == null){// Default to /wwwroot if it exists.var wwwroot = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot");if (Directory.Exists(wwwroot)){hostingEnvironment.WebRootPath = wwwroot;}}else{hostingEnvironment.WebRootPath = Path.Combine(hostingEnvironment.ContentRootPath, webRoot);}if (!string.IsNullOrEmpty(hostingEnvironment.WebRootPath)){hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath);if (!Directory.Exists(hostingEnvironment.WebRootPath)){Directory.CreateDirectory(hostingEnvironment.WebRootPath);}hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath);}else{hostingEnvironment.WebRootFileProvider = new NullFileProvider();}hostingEnvironment.EnvironmentName =options.Environment ??hostingEnvironment.EnvironmentName;}

最终的webRoot 就是在这里创建的

那么如何更改WebRoot和ContentRoot 有2中方式一个是配置文件appsettings.json key值contentRoot , webroot

一个是启动文件修改

Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions

此类包含了IWebHostBuilder 默认扩展

UseContentRoot

UseWebRoot

以上2个方法就是我们在启动的时候可以修改路径的方法都是调用的最终这个方法吧值设置到IConfiguration中 如果是配置文件默认就配置了

public IWebHostBuilder UseSetting(string key, string value)
        {
            _config[key] = value;
            return this;
       }

.net core ContentRoot 和 WebRoot 的区别相关推荐

  1. NET Core 发展及各版本区别

    本月14日,微软发布.NET Core 2.0 正式版,它的发布意味着.NET Core平台更加成熟,也预示其更美好的未来.本文将分析.NET Core 的特性以及未来发展方向,为开发人员选择在何种平 ...

  2. 你注意到 .Net Framework 和 .Net Core 中使用 Session 的区别了吗?

    起因 在测试一个例子时发现的问题,这个示例实现的功能是刷新页面也能保持表格锁定列的状态,先看下页面的完成效果: 测试中发现,几乎相同的代码: 在 FineUIMvc(Net Framework)下没有 ...

  3. 你注意到 .Net Framework 和 .Net Core 中使用 Session 的区别了吗?

    在测试一个例子时发现的问题,这个示例实现的功能是刷新页面也能保持表格锁定列的状态,先看下页面的完成效果: 测试中发现,几乎相同的代码: 在 FineUIMvc(Net Framework)下没有问题: ...

  4. C# .Net Framework、.Net Core和.Net Standard的区别

    .Net Core .NET Core 是一个开源.跨平台的开发平台,可以用来构建现代.可伸缩和高性能的跨平台软件应用程序的,包括Asp.Net Core.WPF.WInform等..Net Core ...

  5. <转载>项目工程中的WebRoot与WebContent有什么区别?

    2018年2月11日 [1] 在MyEclipse中创建web项目后,web程序的根目录文件夹是WebRoot,而创建dynam web project时候,web程序的根 目录文件夹是WebCont ...

  6. 类库(.NET Standard)和类库(.NET Core)区别及选择

    简介: 在VS(Visual Studio)中可以创建3种类库项目(Class Library),包括类库(.NET Framework).类库(.NET Standard).类库(.NET Core ...

  7. ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件

    虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...

  8. Core Graphics

    Core Graphics入门 想必每个第一次接触Core Graphics的开发者都被无数的API.混乱的代码逻辑折腾得头疼不已,甚至望而却步.即使是绘制一个简单的矩形也看上去非常繁琐.本文换一个角 ...

  9. SignalR Core尝鲜

    \ 要点 \\ SignalR Core改用Microsoft.AspNetCore.Sockets,不再依赖HTTP.\\t 使用MessagePack序列化格式,支持二进制协议.\\t TypeS ...

最新文章

  1. RED HAT LINUX 9.0的安装文本模式(2)
  2. Ubuntu解决Nvidia驱动缺失导致的HDMI无法输出问题
  3. Leetcode: Reverse Linked List II
  4. Tomcat 7 安装成功,启动后显示空白页问题
  5. netcore 学习
  6. 多种负载均算法及其 Java 代码实现 --转
  7. OpenCV行人检测
  8. Go 命令行参数,JSON 序列化与反序列化
  9. Collections.toArray方法使用的坑
  10. RabbitMQ introduction
  11. 12000.PAC19XX电源监测芯片
  12. LeetCode(944)——删列造序(JavaScript)
  13. 10个保持注意力的技巧
  14. T-SQL:谓词和运算符(六)
  15. R语言实战应用精讲50篇(十七)--使用R语言实现时间序列分析
  16. [Github] You‘ve successfully authenticated, but GitHub does not provide shell access.
  17. 我在名牌大学毕业后的经历——曾经努力过,就不会后悔
  18. ping计算机名获取IP
  19. ESP32驱动 1.3寸OLED SH1106芯片)
  20. linux 目录比较meld,Meld:Ubuntu下文件/目录对比

热门文章

  1. oh-my-zsh 插件
  2. XSLT是什么类型的语言 1
  3. 中国牙科填料市场调研及投资前景方向预测报告2022-2028年
  4. (免费分享)C#多店进销存管理系统源码 连锁店进销存ERP源码
  5. ps无法新建画布(白色画布一闪而过)
  6. html5 ul下的li重叠解决,firefox中div重叠覆盖之前ul的两种解决方法
  7. springboot+netty 仿微信网页版聊天工具
  8. 商店购物 (shopping.c/cpp/pas)
  9. 解决:Windows无法自动将IP协议堆栈绑定到网络适配器问题(表现形式:电脑突然无法连接互联网,且只剩飞行模式)
  10. 【Javaweb】TCP/IP协议