Selfhosted, use script page with a different root directory

I have a Selfhosted (.NET Core) application that uses the script / template features. How can I change the root contentdirectory to ~/wwwroot so that I can add the html site into the subdirectory wwwroot.

I tried this:
Plugins.Add(new TemplatePagesFeature { VirtualFiles = new FileSystemVirtualFiles("~/templates".MapServerPath()) });

but this does not resolve the index page as the root page, but only as http://*/wwwroot/index.html

Your read/write content root (HostContext.VirtualFiles) should be referencing your host project whilst your publicly servable WebRoot should be configured to wwwroot (HostContext.VirtualFileSources) which is what TemplatePagesFeature uses and should keep using so it’s virtual path matches the /path/info of web requests.

By default it will automatically use wwwroot if you’re using the standard CreateDefaultBuilder() to configure your .NET Core WebHost, e.g:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .Build();

Otherwise if you’re manually configuring it you can use UseWebRoot() to configure it, e.g:

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseWebRoot("wwwroot")
    .UseStartup<Startup>()
    .UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? "http://localhost:5000/")
    .Build();

Please also note that ServiceStack Templates has been re-branded to #Script in the next release:

So if you’re using the v5.4.1 packages on MyGet you should use SharpPagesFeature instead of the deprecated TemplatePagesFeature, although either would work,

But i’m using the SelfHosted App host.
No success by adding this line

public override void Configure(Funq.Container container) {
		this.VirtualFiles = new FileSystemVirtualFiles("~/wwwroot".MapServerPath());

Have you tried the above?

  .UseWebRoot("wwwroot")

As I was saying you don’t want to change VirtualFiles which should point to your Content Root / project folder, only the VirtualFileSources should point to your wwwroot which is automatically configured to use the .NET Core Host WebRoot, so you want to configure that instead.

Where can I configure the UseWebRoot(…) when i’m using a SelfHosted App host?

It’s what configures your .NET Core WebHost in the Program.cs entry point of your App.

This seems to be working. Is this correct in a selfhosted app host?

public override void Configure(Funq.Container container) {
			base.SetConfig(new HostConfig {
				DebugMode = true,
				WebHostPhysicalPath = "wwwroot"
			});

You should ideally be configuring your .NET Core WebHost as above and let ServiceStack inherit the the configured paths.

But i’m not using .NET core webhost!

static void Main(string[] args) {
			var listeningOn = args.Length == 0 ? "http://*:1337/" : args[0];
			var appHost = new AppHost()
					.Init()
					.Start(listeningOn);

			Console.WriteLine("AppHost Created at {0}, listening on {1}",
					DateTime.Now, listeningOn);
			Console.WriteLine("Press any key to stop");
			Console.ReadKey();

and the apphost:

public class AppHost : AppSelfHostBase
	{
		public AppHost()
			: base("HttpListener Self-Host", typeof(MyServices).Assembly) { }

		public override void Configure(Funq.Container container) {
			base.SetConfig(new HostConfig {
#if DEBUG
				DebugMode = true,
#endif
				WebHostPhysicalPath = "wwwroot"
			});

Every .NET Core App needs to configure the .NET Core WebHost which is the entry point of your Program.cs (as mentioned above), e.g. for the self-host project template:

If you’re using AppSelfHostBase the configuration is wrapped, you can override + customize it by overriding:

Thanks! So that’s the preferred way of configuring a different webroot directory. Maybe make the rootdirectory configurable in the AppSelfHost class to prevent overriding the ConfigureHost method. I can’t call the base implementation so whenever you change it, i have to change the override …

Honestly I’d only be using AppSelfHostBase in integration tests (so you can create source-compatible tests for .NET Core/.NET FX), for self-hosted projects I’d be using the standard .NET Core WebHostBuilder and Startup class which you have full control over: