ASP.NET Core MVC

The easiest way to enable Templates support in a .NET Core App is to register an empty ServiceStack AppHost with the TemplatePagesFeature plugin enabled:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseServiceStack(new AppHost());
    }
}

public class MyServices : Service {}

public class AppHost : AppHostBase
{
    public AppHost() : base("ServiceStack Template Pages", typeof(MyServices).GetAssembly()) { }

    public override void Configure(Container container)
    {
        Plugins.Add(new TemplatePagesFeature());
    }
}

This let you use the View Engine and Code Pages features to build your entire .NET Core Web App with Templates without needing to use anything else in ServiceStack.

MVC PageResult

Just as you can use a ServiceStack Service as the Controller for a Template Page View, you can also use an MVC Controller which works the same way as a Service, but instead of returning the PageResult directly, you need to call ToMvcResult() extension method to wrap it in an MVC ActionResult, e.g:

public class HomeController : Controller
{
    public ActionResult Index() =>
        new PageResult(HostContext.TryResolve<ITemplatePages>().GetPage("index")).ToMvcResult();
}

This has the same effect as it has in a ServiceStack Service where the PageResult sets the Content-Type HTTP Response Header and asynchronously writes the template to the Response OutputStream for maximum performance.

Sharing Dependencies

Unfortunately you'd need to resolve ITemplatePages directly from ServiceStack's IOC as dependencies registered in ServiceStack's IOC aren't visible in .NET Core MVC but dependencies registered in .NET Core MVC are visible in ServiceStack so to have the same dependencies available to both .NET Core MVC and ServiceStack you would instead register it in .NET Core's IOC then resolve the same instance in your ServiceStack AppHost, e.g:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var context = new TemplatePagesFeature();
        services.AddSingleton(context);
        services.AddSingleton(context.Pages);
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseServiceStack(new AppHost());
    }
}

public class MyServices : Service {}

public class AppHost : AppHostBase
{
    public AppHost() : base("ServiceStack Template Pages", typeof(MyServices).GetAssembly()) {}

    public override void Configure(Container container)
    {
        Plugins.Add(container.Resolve<TemplatePagesFeature>());
    }
}

This will then let you access ITemplatePages as a normal dependency in your MVC Controller:

public class HomeController : Controller
{
    ITemplatePages pages;
    public HomeController(ITemplatePages pages) => this.pages = pages;

    public ActionResult Index() => new PageResult(pages.GetPage("index")).ToMvcResult();
}

Using Templates without a ServiceStack AppHost

If you don't need the View Engine support and would like to use Templates without a ServiceStack AppHost you can, just register a TemplateContext instance in .NET Core's IOC, replace its Virtual File System to point to the WebRootPath and you can start returning PageResult's as above:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var context = new TemplateContext();
        services.AddSingleton(context);
        services.AddSingleton(context.Pages);
    }

    public void Configure(IApplicationBuilder app)
    {
        var context = app.ApplicationServices.GetService<TemplateContext>();
        context.VirtualFiles = new FileSystemVirtualFiles(env.WebRootPath);
        context.Init();
    }
}

public class HomeController : Controller
{
    ITemplatePages pages;
    public HomeController(ITemplatePages pages) => this.pages = pages;

    public ActionResult Index() => new PageResult(pages.GetPage("index")).ToMvcResult();
}

Checkout the NetCoreApps/MvcTemplates repository for a stand-alone example of this, complete with a sidebar.html partial and a CustomTemplateFilters.cs.

made with by ServiceStack