Scott Hanselman

Botwin offers an interesting alternative option for routing with ASP.NET Core

October 12, 2017 Comment on this post [11] Posted in ASP.NET | Open Source
Sponsored By

NancyFx is a great alternative to ASP.NET if you want to make elegant little web apis like this:

public class SampleModule : Nancy.NancyModule
public SampleModule()
Get["/"] = _ => "Hello World!";

However, it may be that you want a routing style - the way you define your routes - that is like NancyFx BUT you want to use ASP.NET. Botwin is a library that lets you do just that. They say:

This is not a framework, it simply builds on top of Microsoft.AspNetCore.Routing allowing you to have more elegant routing rather than have attribute routing, convention routing, ASP.Net Controllers or IRouteBuilder extensions.

You can plug Botwin into your existing ASP.NET Core application, or you can even add a basic started Botwin app to "dotnet new" like this:

C:\botwinexample> dotnet new -i BotwinTemplate
C:\botwinexample> dotnet new botwin -n MyBotwinApp
C:\botwinexample> dir
10/11/2017 10:14 PM 284 HomeModule.cs
10/11/2017 10:14 PM 470 MyBotwinApp.csproj
10/11/2017 10:14 PM 421 Program.cs
10/11/2017 10:14 PM 408 Startup.cs
4 File(s) 1,583 bytes

You add Botwin as a service to your ASP.NET Core app:

public class Startup
{ public void ConfigureServices(IServiceCollection services) { services.AddBotwin(); } public void Configure(IApplicationBuilder app) { app.UseBotwin(); }

And then add 'Modules' like this:

namespace MyBotwinApp
    using Botwin;
    using Microsoft.AspNetCore.Http;

    public class HomeModule : BotwinModule
        public HomeModule()
            Get("/", async(req, res, routeData) => await res.WriteAsync("Hello from Botwin!"));

That's a hello world. Let's try something more interesting. You can have Before and After hooks like this:

public class HooksModule : BotwinModule
public HooksModule()
this.Before = async (ctx) =>
ctx.Response.StatusCode = 402;
await ctx.Response.WriteAsync("Pay up you filthy animal");
return false;

this.Get("/hooks", async (req, res, routeData) => await res.WriteAsync("Can't catch me here"));

this.After = async (ctx) => await ctx.Response.WriteAsync("Don't forget you owe me big bucks!");

Here's a more complex example. See how they do a BindAndValidate in the Post() where they check for a valid Actor before working with it.

public class ActorsModule : BotwinModule
public ActorsModule(IActorProvider actorProvider)
this.Get("/actors", async (req, res, routeData) =>
var people = actorProvider.Get();
await res.AsJson(people);

this.Get("/actors/{id:int}", async (req, res, routeData) =>
var person = actorProvider.Get(routeData.As<int>("id"));
await res.Negotiate(person);

this.Put("/actors/{id:int}", async (req, res, routeData) =>
var result = req.BindAndValidate<Actor>();

if (!result.ValidationResult.IsValid)
res.StatusCode = 422;
await res.Negotiate(result.ValidationResult.GetFormattedErrors());

//Update the user in your database

res.StatusCode = 204;

this.Post("/actors", async (req, res, routeData) =>
var result = req.BindAndValidate<Actor>();

if (!result.ValidationResult.IsValid)
res.StatusCode = 422;
await res.Negotiate(result.ValidationResult.GetFormattedErrors());

//Save the user in your database
res.StatusCode = 201;
await res.Negotiate(result.Data);

What do you think about the choices you have with ASP.NET Core? Some people feel like the amount of plugability is overwhelming, but I find the flexibility heartening. Go check out Botwin and, hopefully, help out and contribute to open source!

Sponsor: Get the latest JetBrains Rider preview for .NET Core 2.0 support, Value Tracking and Call Tracking, MSTest runner, new code inspections and refactorings, and the Parallel Stacks view in debugger.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 12, 2017 10:21
Nice! This is a lovely way to bring in the succinct elegant way of creating routes / action methods that Nancyfx uses to great effect, for people that don't want to, out aren't able to move away from asp.nrt web API.
October 12, 2017 10:57
This looks really nice, although
seems a little clunky, only in that the route attribute name and type were already stated in the line just above. The ideal would be a strongly-typed "" but I'd be hard pressed to figure out the best way to accomplish that without having to declare a POCO class manually or involving T4 templates.
October 12, 2017 12:12
@Mike C under the covers of As<T> you'll see how ugly it is by default. MVC does this under the hood and adds it to Action method arguments so the generic approach is much better. That said there is work going on for ASPNet Core 2.1 to make this less ugly so fingers crossed we can tidy it up.
October 12, 2017 18:18
Nice to see attribute free web API code.

Are there any examples showing how to post a JSON object with a few fields and an array or two, receive it on the server, call a business method to get a different C# POCO object and return that to the caller?

Is there an example of how to create a default handler for a controller equivalent to handle all routes that are invalid or valid with incorrect input types? ASP.NET MVC is missing that and could output it in the debug output window.
October 13, 2017 0:29
@Greg Sure please see Scott's example of the POST. I'll leave it up to you to call a business method but you can return the object as also shown in the example above to call

As Botwin uses the ASP.NET Core routing you can just specify a route with "/*" as a catch-all route. If your route has an invalid type in a POST/PUT for example the validation will fail and you can check this and return, also shown in the above example. See
October 13, 2017 11:10
I just hope that all this plugability doesn't make MS lazy and slow at evolving and improving ASP.NET MVC.
October 13, 2017 12:15
Stuff like this is what I've been hoping to see since ASP.NET Core was announced. More please!
October 13, 2017 13:12
@Peter Try the new offerings with the pluggability, you might like it and prefer it to MVC ;)
October 13, 2017 21:16
The flexibility is unreal and the "official" MS documentation is quite prescriptive.
My big issue is that .NET is now my work ecosystem and my play ecosystem so my work/life balance is completely shot.
October 13, 2017 22:44
What is the real advantage over MVC?
Performance or something else?
October 16, 2017 3:56
The flexibility in .NET Core is great and reminds me a lot of Java and its ecosystem. That said it's probably going to take some time for the late majority of the .NET community to adopt and embrace it.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.