ASP.NET Core MVC

The easiest way to enable #Script support in a .NET Core App is to register an empty ServiceStack AppHost with the SharpPagesFeature 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 Sharp Pages", typeof(MyServices).GetAssembly()) { }

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

This let you use the Sharp Pages and Sharp 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 Sharp 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<ISharpPages>().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 ISharpPages 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 SharpPagesFeature();
        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 Sharp Pages", typeof(MyServices).GetAssembly()) {}

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

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

public class HomeController : Controller
{
    ISharpPages pages;
    public HomeController(ISharpPages 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 Sharp Pages support and would like to use Templates without a ServiceStack AppHost you can, just register a ScriptContext 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 ScriptContext();
        services.AddSingleton(context);
        services.AddSingleton(context.Pages);
    }

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

public class HomeController : Controller
{
    ISharpPages pages;
    public HomeController(ISharpPages 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 CustomScriptMethods.cs.

 

ASP.NET v4.5 Framework MVC

You can return a Sharp Page in a classic ASP.NET MVC similar to ASP.NET Core MVC Controller except that in order to work around the lack of being able to async in classic ASP.NET MVC Action Results you need to return a task, but because we can replace the IOC Controller Factory in ASP.NET MVC you can use Constructor Injection to access the ISharpPages dependency:

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

    public Task<MvcPageResult> Index() =>
        new PageResult(pages.GetPage("index")).ToMvcResultAsync();
}

ASP.NET MVC + Templates Demo

Checkout the ServiceStackApps/MvcTemplates repository for a stand-alone example, complete with a sidebar.html partial and a CustomScriptMethods.cs.

This demo was created from the ServiceStack ASP.NET MVC5 Empty VS.NET Template.

made with by ServiceStack