发布:2023/12/7 15:40:05作者:大数据 来源:大数据 浏览次数:657
ASP.NET Core 已经发布了 2.0 RC1 (Go Live) 版本, API 已经不在变化, 但是很多人期待的已久的在虚拟目录中部署的功能还是没有出现, 看来只能自己想办法解决这个问题了。
由于 ASP.NET Core 有自己的服务器进程 (kestrel) , 在部署时通常是采用反向代理转发的形式, 对于应用服务器的集成上, 不如传统的 ASP.NET 应用那么友好, 比如在应用服务器的虚拟目录中部署, 一直没有很好的方案。
ASP.NET Core 有一个 UsePathBase 扩展方法, 使用一个中间件向当前 http 请求中添加或者移除根路径 (Pathbase) , 利用这个方法, 添加虚拟路径也就不难实现。
首先来定义并导出一个名称为 ASPNETCORE_PATHBASE
的环境变量,
1 2 |
<span class="nb"><span class="hljs-attribute">export</span> </span><span class="nv">ASPNETCORE_PATHBASE</span><span class="o">=</span>/MvcTestApp |
再在 Startup.cs
文件中的 Configure
方法的最前面, 添加下面的代码, 读取这个环境变量, 将环境变量的值作为 PathBase
中间件, 代码如下所示:
1 2 3 4 5 6 7 8 9 10 |
<span class="k"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="k"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span> <span class="nf"><span class="hljs-function"><span class="hljs-title">Configure</span></span></span><span class="p"><span class="hljs-function">(</span></span><span class="n"><span class="hljs-function"><span class="hljs-params">IApplicationBuilder</span></span></span> <span class="n"><span class="hljs-function"><span class="hljs-params">app</span></span></span><span class="p"><span class="hljs-function"><span class="hljs-params">,</span></span></span> <span class="n"><span class="hljs-function"><span class="hljs-params">IHostingEnvironment</span></span></span> <span class="n"><span class="hljs-function"><span class="hljs-params">env</span></span></span><span class="p"><span class="hljs-function">)</span></span> <span class="p">{</span> <span class="c1"><span class="hljs-comment">// 读取环境变量 ASPNETCORE_PATHBASE</span></span> <span class="kt"><span class="hljs-keyword">var</span></span> <span class="n">pathBase</span> <span class="p">=</span> <span class="n">Environment</span><span class="p">.</span><span class="nf">GetEnvironmentVariable</span><span class="p">(</span><span class="s"><span class="hljs-string">"ASPNETCORE_PATHBASE"</span></span><span class="p">);</span> <span class="c1"><span class="hljs-comment">// 如果 ASPNETCORE_PATHBASE 的值不为空, 则使用 Pathbase 中间件</span></span> <span class="k"><span class="hljs-keyword">if</span></span> <span class="p">(!</span><span class="kt"><span class="hljs-keyword">string</span></span><span class="p">.</span><span class="nf">IsNullOrEmpty</span><span class="p">(</span><span class="n">pathBase</span><span class="p">))</span> <span class="p">{</span> <span class="n">app</span><span class="p">.</span><span class="nf">UsePathBase</span><span class="p">(</span><span class="k"><span class="hljs-keyword">new</span></span> <span class="nf">PathString</span><span class="p">(</span><span class="n">pathBase</span><span class="p">));</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s"><span class="hljs-string">"Hosting pathbase: "</span></span> <span class="p">+</span> <span class="n">pathBase</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> |
注意, 一定要在 Configure 方法的最前面调用这个方法, 让 Pathbase 中间件成为第一个处理请求的中间件, 才能正确的模拟虚拟路径。
现在输入命令 dotnet run
, 开始运行, 输出如下
1 2 3 4 5 6 7 |
Hosting <span class="hljs-string">pathbase:</span> /MvcTestApp Hosting <span class="hljs-string">environment:</span> Development Content root <span class="hljs-string">path:</span> <span class="hljs-regexp">/Projects/</span>MvcTestApp Now listening <span class="hljs-string">on:</span> <span class="hljs-string">https:</span><span class="hljs-comment">//localhost:5001</span> Now listening <span class="hljs-string">on:</span> <span class="hljs-string">http:</span><span class="hljs-comment">//localhost:5000</span> Application started. Press Ctrl+C to shut down. |
现在, 可以通过 http://localhost:5000/MvcTestApp/
来访问这个应用了。
如果使用 Visual Studio 或者 VS Code 进行调试, 则需要修改 Properties
目录下的 launchSettings.json
文件, 为不同的 profile 均添加一个 ASPNETCORE_PATHBASE 环境变量, 如下所示:
1 2 3 4 5 6 7 |
<span class="p">{</span> <span class="nl"><span class="hljs-attr">"environmentVariables"</span></span><span class="p">:</span> <span class="p">{</span> <span class="nl"><span class="hljs-attr">"ASPNETCORE_ENVIRONMENT"</span></span><span class="p">:</span> <span class="s2"><span class="hljs-string">"Development"</span></span><span class="p">,</span> <span class="nl"><span class="hljs-attr">"ASPNETCORE_PATHBASE"</span></span><span class="p">:</span> <span class="s2"><span class="hljs-string">"/MvcTestApp"</span></span> <span class="p">}</span> <span class="p">}</span> |
部署到生产环境下, 如果需要修改虚拟目录的值, 则只要调整环境变量 ASPNETCORE_PATHBASE
的值就行了, 不需要修改代码。
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4