c#-AspNetCore集内容根路径

c#-AspNetCore集内容根路径,c#,asp.net,asp.net-core,blazor,C#,Asp.net,Asp.net Core,Blazor,如何在app.Net Core 3.0 ASP Blazor start上更改内容根路径? 现在应用程序以输出开始 info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0] User profile is available. Using 'C:\Users\Art\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and W

如何在app.Net Core 3.0 ASP Blazor start上更改内容根路径? 现在应用程序以输出开始

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\Art\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
!!! C:\Program Files\WindowsApps\6c2bb0ad-5956-4886-9e3f-2135ebe50d2f_1.0.8.0_x64__n37t8n8dtxdg6\TUTDF_Viewer_v2\
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Windows\system32
在应用程序初始化时,我需要将
内容根路径
C:\Windows\system32
更改为另一个路径

如何更改AspNetCore应用程序开始时的
内容根路径?

请使用以下代码

    public static async Task Main(string[] args)
    {
        string pathToContentRoot = string.Empty;

        var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        pathToContentRoot = Path.GetDirectoryName(pathToExe);
        Directory.SetCurrentDirectory(pathToContentRoot);
    }

最正确的方法是将项目的Program.cs更改为add

var p = System.Reflection.Assembly.GetEntryAssembly().Location;
                    p = p.Substring(0, p.LastIndexOf(@"\") + 1);
webBuilder.UseContentRoot(p);
CreateHostBuilder

完整示例:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    var p = System.Reflection.Assembly.GetEntryAssembly().Location;
                    p = p.Substring(0, p.LastIndexOf(@"\") + 1);

                    webBuilder.UseContentRoot(p);
                    webBuilder.UseStartup<Startup>();
                });
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
var p=System.Reflection.Assembly.GetEntryAssembly().Location;
p=p.Substring(0,p.LastIndexOf(@“\”)+1);
webBuilder.UseContentRoot(p);
webBuilder.UseStartup();
});

似乎不适用于ASP.NET核心MVC v3.1项目。我们的解决方案就是将当前目录更改为webapp目录,然后用dotnet从那里启动.dll