Scott Hanselman

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

October 12, '17 Comments [14] 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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Thursday, 12 October 2017 06:21:02 UTC
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.
Thursday, 12 October 2017 06:57:44 UTC
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.
Mike C
Thursday, 12 October 2017 08:12:09 UTC
@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.
Thursday, 12 October 2017 14:18:16 UTC
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.
Thursday, 12 October 2017 20:29:30 UTC
@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
Friday, 13 October 2017 07:10:30 UTC
I just hope that all this plugability doesn't make MS lazy and slow at evolving and improving ASP.NET MVC.
Friday, 13 October 2017 08:15:33 UTC
Stuff like this is what I've been hoping to see since ASP.NET Core was announced. More please!
Friday, 13 October 2017 09:12:04 UTC
@Peter Try the new offerings with the pluggability, you might like it and prefer it to MVC ;)
Friday, 13 October 2017 17:16:05 UTC
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.
Fergal Moran
Friday, 13 October 2017 18:44:47 UTC
What is the real advantage over MVC?
Performance or something else?
Sunday, 15 October 2017 23:56:56 UTC
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.
Tuesday, 17 October 2017 04:09:48 UTC
You are so interesting! I don't suppose I have read through a
single thing like this before. So nice to find someone with some original
thoughts on this subject matter. Really.. thank you for starting this
up. This website is one thing that is required on the web, someone with a little originality!
Tuesday, 17 October 2017 13:38:47 UTC
thanks for sharing this tuts
Tuesday, 17 October 2017 19:32:28 UTC
salut wot cd u faire suivant apres que plz jeanmicheljarre de phil
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

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