Using Templates as a replacement for Razor with Content Negotiation

Is there a way that Templates can be as the rending engine for only text/html requests? Currently it looks like the service has to be dedicated to returning pages as a PageResult needs to be returned rather than a DTO. Where previously it was possible to return a DTO and the appropriate rendering/serialization was performed based on the accepted content types.

Templates doesn’t fit into the existing ViewEngine model used for Razor, i.e there’s no base page with a generic TModel to hold the Response DTO or /Views folder concept where View pages are stored in.

But with the new Content-Type Specific Service Implementation feature you can easily use Templates to handle HTML requests with:

public class MyServices : Service
{
    public object Any(MyRequest request) => ...;

    public object AnyHtml(MyRequest request) => 
        new PageResult(Request.GetPage("path/to/my-request")) { Model = Any(request) };
}

FYI I’ve just added initial support for using TemplatePages View Pages to render the HTML for Service Responses. It works similarly to Razor ViewPages where it uses first matching View Page where the Response DTO is injected as the Model property.

The View Pages can be in any folder in the /Views folder with the format {PageName}.html where PageName can be either the Request DTO Name, Response DTO Name or HttpResult.View property if returning a custom HttpResult. All page names need to be unique in the /Views folder.

One difference from Razor is that it uses a cascading _layout.html instead of /Views/Shared/_Layout.cshtml. So if your view page was in:

/Views/dir/MyRequest.html

It will use the closest _layout.html it can find starting from:

/Views/dir/_layout.html
/Views/_layout.html
/_layout.html

This is now available from v4.5.15 that’s now available on MyGet.

Disclaimer: this code is super fresh so there may be bugs! Please report any you find :slight_smile:

This is awesome :slight_smile: Thanks! I shall give it a go.