Pages

Build a minimal API using ASP.Net Core with Android Studio Code

In order to create HTTP APIs with less dependencies, minimal API architecture is perfect. If you want to build API for applications and microservices which include only a few features, files, and dependencies in ASP.Net Core API with Android Studio Code


Earlier we created a blog on Minimal API build using ASP.Net Core with Android Studio. In this article, we have mentioned step-by-step to build API using ASP.Net Core. The step for building API with Android Studio and Android Studio is similar. Build ASP.Net Core Web API by following the below steps. 

Overview

We have briefed the ASP.Net Core tutorial, with the help of which you can build API. The ASP.Net Core produces responseType API that allows you to analyze different type of data contained in the response. 


API

Description

Request body

Response body

GET /todoitems

Receive all to-do items

None

Array of to-do items

GET /todoitems/{id}

Receive an item by ID

None

To-do item

DELETE /todoitems/{id}

Delete an item

None

None

PUT /todoitems/{id}

Updating an existing item

To-do item

None

POST /todoitems

Add a new item

To-do item

To-do item

GET /todoitems/complete

Get a completed to-do items

None

Array of to-do items

Prerequisites

Before getting into the steps, lets first setup the environment.

Visual Studio Code

C# for Visual Studio Code

.NET 7.0 SDK


The ASP.NET Core development instructions in Visual Studio Code leverage the.NET CLI for tasks like project creation. Any code editor and macOS, Linux, or Windows can be used to carry out these instructions. If you use a program other than Visual Studio Code, a few small adjustments could be necessary.

Step 1: Create an API project

First open the integrated terminal.

Change all the directories (cd) to the folder which will include the project folder. 

Run the below-given command.


.NET CLI

dotnet new web -o TodoApi

cd TodoApi

code -r ../TodoApi


A new dialog box will appear whether you want to trust the authors, select the option ‘Yes’.


After that new dialog box will appear asking whether you want to add the required assets to the new project, select the ‘Yes’ option. 

Step 2: Examine the code

The following code is stored in Program.cs file. The following C# code will help you build a basic Web API.


C#

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();


app.MapGet("/", () => "Hello World!");


app.Run();


The aforementioned code:

Step 3: Run and test your app

The HTTP development trust certificate by running the below-given code.


.NET CLI

dotnet dev-certs https --trust


  • The aforementioned command does not run on Linux. Look after your Linux distribution’s documentation in order to trust a certificate. 


  • After running the command you will get the below-given dialog box. The trust certificate will be generated which was not generated earlier. 


                                                Source: learn.microsoft.com


  • Select the ‘Yes’ option if you agree with the terms and conditions of the development condition.



  • You will get a ‘Hello World!’ message on your browser. Minimal data is stored in Program.cs but it is a complete app. 

Step 4: Add NuGet packages

Add NuGet packages to support diagnostics and database which is used in this guide. 


Run the below-given command.


.NET CLI

dotnet add package Microsoft.EntityFrameworkCore.InMemory

dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore


To get ‘Steps 5 and 6’ visit our blog on Minimal API using ASP.NET Core with Android Studio. All the information is the same as ASP.Net Core API with Android Studio from the 5th step onward. 


No comments:

Post a Comment

Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...