Take advantage of the lightweight and fast Nancy framework for building HTTP-based services in .Net Core Credit: Thinkstock Nancy is a lightweight framework for building HTTP-based services. Nancy prefers conventions over configuration and provides support for GET, HEAD, POST, PUT, DELETE, and PATCH operations. Nancy is also open source under the MIT license. This article presents a discussion of how we can use Nancy with an ASP.Net Core application. Nancy is a web framework and has no dependencies on System.Web or other .Net libraries. Most importantly, you are not constrained to adhering to the MVC pattern or any other pattern if you are using Nancy. Nancy is just a service endpoint that can respond to HTTP verbs. This makes Nancy a good choice for building websites, APIs, and web services. Nancy is host-agnostic. You can run it in IIS, in WCF, as a Windows Service, embedded within an .exe file, or inside a self-hosted application. Nancy is quite easy to set up and customize. Another advantage of Nancy is its built-in support for dependency injection. Nancy also provides a library that can be used for testing the request-response cycle easily. I’ll discuss this feature of Nancy in a later post. Create an ASP.Net Core project in Visual Studio First off, let’s create an ASP.Net Core project in Visual Studio. If you don’t have Visual Studio 2019 installed in your system, you can download it here. To create a new ASP.Net Core project in Visual Studio 2019, follow the steps given below. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “ASP.Net Core Web Application” from the list of templates displayed. Click Next. In the “Configure your new project” window, specify the name and location for the new project. Click Create. In the “Create New ASP.Net Core Web Application” window, select .Net Core as the runtime and ASP.Net Core 2.2 (or later) from the drop-down list at the top. Select “Web Application” as the project template. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here. Ensure that Authentication is set to “No Authentication” as we won’t be using authentication either. Click Create. You should now have a new ASP.Net Core project ready to go in Visual Studio. We’ll use this project in the sections below to build our custom hosted service. Install and configure Nancy in ASP.Net Core To install Nancy, right-click on your project in the Solution Explorer window and select “Manage NuGet packages…”. Then, in the NuGet Package Manager window, search for Nancy and install it. Alternatively, you can install Nancy from the NuGet Package Manager console using the following command. Install-Package Nancy Once Nancy has been installed, the next thing you should do is configure Nancy. To do this, you should call the UseNancy method in the Configure method of the Startup class as shown below. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); app.UseOwin(x => x.UseNancy()); } Create your first Nancy module in ASP.Net Core So far so good. Let’s now create a Nancy module and write some code for it. A Nancy module is a standard C# class that extends the NancyModule class of the Nancy framework. public class HomeModule : NancyModule { } It should be noted that you must declare your Nancy module as public. The Nancy framework cannot discover a module that isn’t marked as public. Create routes in a Nancy module in ASP.Net Core A Nancy module defines the routes in its constructor. To define a route in Nancy, you should specify the HTTP verb, the pattern, the action, and (optionally) the condition. Here is an example that illustrates a Nancy route definition. public class HomeModule : NancyModule { public HomeModule() { Get("/", args => GetAllAuthors()); Get("/{id:int}", args => GetAuthorById(args.id)); } } In essence, a Nancy module is a place for defining HTTP endpoints. The following code snippet illustrates a Nancy module that can handle three different GET requests. public class HomeModule : NancyModule { public HomeModule() { Get("/", args => "Welcome to Nancy."); Get("/Test", args => "Test Message."); Get("/Hello", args => $"Hello {this.Request.Query["name"]}"); } } Nancy is not only lightweight, modular, and fast, but installing and configuring it is quite easy. You can use Nancy to provide essential HTTP services with minimal effort. To learn more about the Nancy framework, you can refer to the documentation on GitHub. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos