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:
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,
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.
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:
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: