Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create and Run a WebAPI on Windows Using .NET Core

A WebAPI (Web Application Programming Interface) is a crucial component in modern software development, allowing different software systems to communicate over the internet. In the Windows environment, creating and running a WebAPI can be efficiently achieved using .NET Core. This article will guide you through the process of setting up a WebAPI on a Windows machine, highlighting the importance of WebAPIs in enabling seamless integration between different applications and services.


Examples:


1. Setting Up Your Environment:


Before you start, ensure that you have the following installed on your Windows machine:



  • .NET Core SDK


  • Visual Studio or Visual Studio Code


    You can download and install the .NET Core SDK from the official Microsoft website. Once installed, you can verify the installation by running the following command in Command Prompt (CMD):


    dotnet --version



2. Creating a New WebAPI Project:


Open CMD and navigate to the directory where you want to create your project. Run the following command to create a new WebAPI project:


   dotnet new webapi -n MyWebAPI

This command creates a new directory named MyWebAPI with a basic WebAPI template.


3. Building and Running the WebAPI:


Navigate to the project directory:


   cd MyWebAPI

Build the project using:


   dotnet build

Run the project with:


   dotnet run

By default, the WebAPI will be hosted on http://localhost:5000 and https://localhost:5001. You can open these URLs in your browser to see the default API response.


4. Creating a Simple API Endpoint:


Open the project in Visual Studio or Visual Studio Code. Navigate to the Controllers folder and open the WeatherForecastController.cs file. You can add a new endpoint like this:


   [HttpGet("hello")]
public IActionResult GetHello()
{
return Ok("Hello, World!");
}

Save the file and restart the application using dotnet run. You can now access the new endpoint at http://localhost:5000/weatherforecast/hello.


5. Testing the API:


You can use tools like Postman or CURL to test your API endpoints. For example, using CURL in CMD:


   curl http://localhost:5000/weatherforecast/hello

This should return "Hello, World!".


To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.