4

How can I change Content root path on app .Net Core 3.0 ASP Blazor start? Now app starts with output

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

I need to change Content root path from C:\Windows\system32 to another path on app initialization.

How to change Content root path on AspNetCore app start?

4 Answers 4

4

The most correct way is to change Program.cs of the project - to add

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

in CreateHostBuilder.

Full example:

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>();
                });
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't seem to work for an ASP.NET Core MVC v3.1 project. The solution for us was just to change current directory to the webapp directory and start the .dll from there with dotnet
1

Please use below code

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

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

Comments

0

Directory.SetCurrentDirectory only works for loading razor content and config.

If you have static files (UseStaticFiles) as middleware active, you must have somewhere this too.

           env.ContentRootPath = pathToContentRoot;

Comments

0

on ASP7 we inserted following code to make it work into program.cs

WebApplicationOptions options = new WebApplicationOptions {
    ContentRootPath = "/var/www/yourpath"
};
var builder = WebApplication.CreateBuilder(options);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.