Making a minimal webapp with dotnet core
Recently I wanted to make myself a short url host. Really, I made this not to make short urls, but to make memorable urls for myself.
I wanted to say goto aka.terribledev.io/docker101 for my docker101 class
This is clearly a very simple webapp. Basically a dictionary of path’s to 301’s. Probably the simplest WebApplication anyone can make. So I made it.
So, I’m sure you are wondering. Well why am I reading this blog? Well, my app does not use mvc, or any web framework. My app uses the mvc core 1.0 router as middleware. This was talked about a while back on an episode of live.asp.net.
I first just used the aspnet yeoman generator, asking for webapi. I then deleted the only controller I was given, and got rid of most of the mvc packages with the exception of
Microsoft.AspNetCore.Routing. I then just modified my
Startup.cs file to look like below.
Essentially I add the routing package to the container, and then have have the app use the router mapping
docker101 to the proper url. Easy right?
Ok, so what is the result? Well, extremely fast url parsing thanks to the mvc router, and a really lightweight application. Since we have no session, or even convention based action resolution. Our app is extremely thin, and fast.
So, thank Microsoft for really making the components mvc modular. This provides a very small, and lightweight service!