JasperFx 0.9.8


Next

Reverse Url Lookup

Previous

Jasper.MvcExtender

Advanced HTTP Configuration Edit on GitHub


This topic is a grab bag of rarely used options, but stuff you may want to exploit for more advanced scenarios. Honestly, if you find yourself wanting to use something here, ask any questions you might have in the Jasper Gitter room as linked in the header above.

"Fast" Mode

ASP.Net Core has middleware applied automatically to open a scoped IoC container for an HTTP request before any other middleware is executed, such that every bit of middleware and MVC Core itself can use a single IoC request scope all the way through that request. Great, but that's generally unnecessary for Jasper actions and maybe just a waste of server resources. To go a little faster in Jasper, we've got a setting that will reach into your ASP.Net Core configuration and rip that silly thing out:


// Idiomatic Jasper bootstrapping
var runtime = JasperHost.For(x =>
{
    x.HttpRoutes.AspNetCoreCompliance = ComplianceMode.GoFaster;
});

// Or the ASP.Net Core way
var host = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    .UseJasper(x => x.HttpRoutes.AspNetCoreCompliance = ComplianceMode.GoFaster)
    .Start();

Disable HTTP Routes Altogether

Meh, maybe you don't want the Jasper routing to be in effect whatsoever and only use Jasper for its messaging or command execution. In that case, you can disable all route discovery like so:


public class NoHttpRoutesApp : JasperRegistry
{
    public NoHttpRoutesApp()
    {
        HttpRoutes.Enabled = false;
    }
}

or with IWebHostBuilder as the bootstrapping:


var host = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    .UseJasper(x => x.HttpRoutes.Enabled = false)
    .Start();

Policies

To apply middleware to Jasper routes in a conventional way, you can use the IRoutePolicy interface like this:


public interface IRoutePolicy
{
    void Apply(RouteGraph graph, JasperGenerationRules rules);
}

As an example, the Jasper.MvcExtender library has a custom policy to add some Jasper middleware around endpoint actions implemented by ControllerBase classes for some MVC Core compliant runtime actions:


internal class ControllerUsagePolicy : IRoutePolicy
{
    public void Apply(RouteGraph graph, JasperGenerationRules rules)
    {
        graph
            .Where(x => x.Action.HandlerType.CanBeCastTo<ControllerBase>())
            .Each(x =>
            {
                x.Middleware.Add(new BuildOutControllerContextFrame());
                x.Middleware.Add(new SetControllerContextFrame(x.Action.HandlerType));
            });
    }
}

To actually apply a route policy, you can add it to the JasperRegistry.HttpRoutes.GlobalPolicy() method as shown below:


registry.HttpRoutes.GlobalPolicy<ControllerUsagePolicy>();

Reader/Write Rules

Maybe just for Jasper development itself, but there's an extension point that will allow you to customize how Jasper generates code inside of its RouteHandler classes for certain input types or resource types. Here's the built in writer rule for string resource types:


public class WriteText : IWriterRule
{
    public bool TryToApply(RouteChain chain)
    {
        if (chain.ResourceType != typeof(string)) return false;

        chain.Postprocessors.Add(new CallWriteText(chain.Action.ReturnVariable));

        return true;
    }
}

To register the custom rules, you can register IWriterRule or IReaderRule services with the IoC service registrations.

Ignore Endpoint or Controller Actions

You can mark any type or method with the [JasperIgnore] attribute that tells Jasper not to use that whole class or individual method as an HTTP action method.