August 20, 2016 by Anuraj
C# asp.net core NancyFx
This post is about using NancyFx in ASP.NET Core. NancyFx is lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono. The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions. Nancy is designed to handle DELETE, GET, HEAD, OPTIONS, POST, PUT and PATCH requests and provides a simple, elegant, Domain Specific Language (DSL) for returning a response with just a couple of keystrokes. Integration with NancyFx was available on earlier days (k days) for ASP.NET Core, but in DNX days it lost the support and now it is back.
This is an empty asp.net core application, in which you need to add Nancy references. Here is the project.json file.
The “Microsoft.AspNetCore.Owin” package is important, which will help you to use “UseOwin” method. There is no change in the program.cs file, it is same. Instead of ASP.NET runtime handling, you need to use Nancy to handle the requests. So you need to change the Configure method inside Startup class like this.
And you need to create a Nancy module to handle the requests. Here is the code for HomeModule class.
This above code will handle requests to the / and will return “Hello World” as plain text.
Here is the response using POSTMAN
And here is the code for handling a POST request.
this.Bind method will help you to do model binding.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub