Sunday, October 15, 2023

.NET 7 Minimal APIs Series - An Introduction

Introduction to .NET 7 Minimal APIs

The release of .NET 6 brought about several exciting features and improvements, and one of the most exciting additions is the Minimal API. Minimal APIs make it easier than ever to create lightweight and efficient web applications in .NET. With its immense usefulness, I have started this .net 7 minimal API series and this is the first part of it.

In this blog post, we'll take a closer look at what Minimal APIs are and how you can get started with them.

What are Minimal APIs?

Minimal APIs are a new way to build web applications using .NET. They are designed to be simple, lightweight, and highly productive. With Minimal APIs, you can create web endpoints with minimal code, making it an ideal choice for small to medium-sized projects, microservices, or scenarios where a traditional MVC framework might be overkill. There main advantages are :

Reduced Boilerplate Code: 

Minimal APIs eliminate much of the boilerplate code required in traditional ASP.NET web applications, resulting in cleaner and more concise code.

Lightweight: 

Minimal APIs are designed to be lightweight, which means your applications have lower memory and startup time overhead.

Improved Performance: 

The reduced complexity often leads to improved performance, making Minimal APIs a great choice for high-performance applications.

Ease of Use: 

They are easy to understand, especially for developers who are new to .NET or web development. You can quickly get started and build your API with just a few lines of code.

Integration with Existing Code:

You can seamlessly integrate Minimal APIs into existing .NET projects, making them a versatile choice for a wide range of scenarios.
Getting Started with Minimal APIs

To get started with Minimal APIs, you'll need .NET 6 SDK and Visual Studio 2022 or your preferred code editor. Here's a step-by-step guide to creating a simple Minimal API:

Step 1: Create a New .NET 7 Project

You can create a new project using the following command:

bash
dotnet new web -n MyMinimalApi
Or alternatively, open Visual Studio 2022, and select the Asp.Net Web API project from Microsoft from the templates list.



And once selected, in the next step, keep the "Use controllers" checkbox unchecked.




Step 2: Define Endpoints

In your Program.cs file, you'll define your endpoints using C# 10's new top-level statements. For example, a simple "Hello, World!" endpoint might look like this:

csharp
var builder = WebApplication.CreateBuilder(args); 
var app = builder.Build(); 
 app.MapGet("/", () => "Hello, World!"); 
 app.Run();

Step 3: Run Your Application

You can run your application with the following command:

bash
dotnet run
Your Minimal API is now up and running! You can access it by navigating to http://localhost:5000 in your web browser.

Conclusion

.NET Minimal APIs offer a straightforward and efficient way to build web applications in .NET. Whether you're a beginner or an experienced developer, Minimal APIs can simplify your development process, improve performance, and reduce boilerplate code. They are a powerful addition to the .NET ecosystem, and you should consider using them for your next project.

This introduction should provide you with a solid foundation to start exploring and working with Minimal APIs. As you become more familiar with the concept, you can delve into more advanced topics and features that .NET 7 offers to create robust web applications.

Stay tuned for more in-depth tutorials and examples to help you make the most of Minimal APIs in .NET 7!


Sunday, February 27, 2022

Lets play with cypress - Part 2

 In the first part I explained starting of my automation journey with first command to setup cypres at the end i.e. npm install cypress -g Next steps were to create a project with npm init commad and as pre-requisite I had node installed already. 


After init command I provided required information through command prompt and within a minute I had my project created and I opened it in vs code through code . command on cmd.



Once vs code is opened with my project loaded in it, next command was cypress open and magic happened. Default cypress template get generated with basic and advanced examples folders in it. 


As we can see, cypress has particular project structure and by default we used to write our tests in side integration folder. We can divide our tests into folders based of features as this separate is logical so its totally upon us who confortable we are in that decision. I know thats quick one but that is all for part 2 where we have created a project with default test examples, code if this test project can be found on my github repository here : https://github.com/musmanrafiq/cypress/tree/main/normal where I am already ahead of my blog post. Stay tuned for the next part.

Sunday, February 20, 2022

Lets play with cypress - Part 1

 As tech geek I am a big fan of automating stuff, where stuff means, software development related activities 😆 in context of this post, its software testing automation. It all started when I fist used postman to test my .net based web Restful API's and while playing with that awesome tool, I came to know, it has its brother newman (cli tool) as well. Long story short, it bacame a hebit to make collections of my apis and smooke them after every release in CI/CD through newman and run test on my machine through postman. I think this is enough for building the basis, lets swith the context a little bit.

Recently as a team we decided to choose cypress as testing automation framework amongst couple of others based of following points:

- Its javascript framework, can be used by a developer or a QA to write tests which require basic knowledge of javascript.

- Supports vast varity of tests i.e. End-to-end, Integration and unit tests.

- More attractive features i.e. Network traffic control, automatic waits and screenshoots / videos are most popular ones.

Before I write Part2 of this learning, I must confess, before writing my first automated test, I ran :

npm install cypress -g

Exactally, I installed cypress as global package.  Lets see, what if we install it in project scope later down in this series.

Sunday, September 26, 2021

Area Routing in asp.net dotnet5 - BlogEngine

 Areas are small function units consists of Controllers, Views and models. Assume as scenario where we have two roles lets say, User and Admin. We know that these two roles require different views on website and hence different functionality. Admin has full control and user can only view or add comments on a post. This scenario require to have a separate area for Admin where we can manage its own functionality and UI. 

In Asp.net dotnet5 we can add area by right clicking on project and click on Add and select Area. And from routing point of view its very important to configure area correctly.

 

For complete project checkout Asp.net Dotnet5 Blog Engine https://github.com/musmanrafiq/BlogEngine

.NET 7 Minimal APIs Series - An Introduction

Introduction to .NET 7 Minimal APIs The release of .NET 6 brought about several exciting features and improvements, and one of the most exci...