Jasper.MvcExtender Edit on GitHub


Note! This extension is brand spanking new, and only supports a subset of MVC Core. It would be very, very helpful to have some early adopters kick the tires on this.

Rather than reinvent the whole MVC Core world, Jasper has an extension library called Jasper.MvcExtender that allows you to use a subset of MVC Core artifacts within Jasper endpoints and even to use MVC Core Controller classes within Jasper's more efficient runtime pipeline. The only thing you need to do to utilize this add on is to make the Nuget reference and Jasper will automatically discover the extension and apply it to your Jasper configuration.

See Extensions for more information about how Jasper extensions work.

Controller and ControllerBase

The presence of the Jasper.MvcExtender extension allows Jasper to automatically discover endpoint actions on concrete classes that inherit from MVC Core's ControllerBase type. The action methods can either follow Jasper's idiomatic routing naming convention like this example:


public class ControllerUsingJasperRouting : Controller
{
    // Use idiomatic Jasper routing
    // This would respond to "GET: /stuff/other"
    public string get_stuff_other()
    {
        return "other stuff";
    }
}

Or, you can use the MVC Core attributes for expressing the routing patterns and Jasper will consider any method marked with one of these attributes as an HTTP action method. Here's an example:


public class ControllerUsingMvcRouting : ControllerBase
{
    public string get_stuff()
    {
        return "stuff";
    }

    [HttpPost("one")]
    public int Post1()
    {
        return 200;
    }

    [HttpGet("/one")]
    public string Get1()
    {
        return "one";
    }

    [HttpGet("/dog/{name}")]
    public string GetDog(string name)
    {
        return $"the dog is {name}";
    }
}

Lastly, Jasper has limited support for the [Route] attribute like this:



[Route("api/[controller]")]
public class TodoController : ControllerBase
{
    [HttpGet]
    public string GetList()
    {
        return "ok";
    }


    // Responds to "POST: /api/todo"
    [HttpPost]
    public int Post()
    {
        return 200;
    }
}

Lastly, the ControllerBase.HttpContext property is set during the course of executing a ControllerBase action as shown here:


[HttpGet("write")]
public Task WriteIntoTheContext()
{
    return HttpContext.Response.WriteAsync("I wrote some stuff here");
}

IActionResult

With the Jasper.MvcExtender added to your project, Jasper can use IActionResult objects as the resource type like this:


[HttpGet("result")]
public IActionResult Result()
{
    return StatusCode(202);
}

[HttpGet("json")]
public JsonResult WriteJson()
{
    return new JsonResult(new Hero
    {
        Name = "Wolverine",
        Affiliation = "Xmen"
    });
}