Web API study notes (1) - introduction and creation

Introduction

1. REST in ASP.NET Core

When you browse the web, the web server communicates with the browser using HTML, CSS, and JavaScript. If you interact with the page in some way, such as submitting a login form or selecting a buy button, the browser sends information back to the web server.
Likewise, web servers can communicate with a wide variety of clients (browsers, mobile devices, other web servers, etc.) using web services. API clients communicate with servers via HTTP, and both use data formats such as JSON or XML to exchange information. APIs are typically used by single-page applications (SPAs) to perform most of the user interface logic in a web browser. Communication with the web server is primarily through the web API.

2. REST: A common pattern for generating APIs using HTTP

Representational State Transfer (REST) ​​is an architectural style for generating Web services. REST requests are made over HTTP. They use the same HTTP verbs that web browsers use to retrieve web pages and send data to servers. The verbs are as follows:
GET: Retrieve data from the web service.
POST: Creates a new data item on the web service.
PUT: Updates a data item on a web service.
PATCH: Updates a data item on a Web service by describing a set of instructions on how to modify the item. The sample applications in this module do not use this verb.
DELETE: Deletes a data item on the web service.
A web service API that follows REST is called a RESTful API. They are defined by:
A base URI.
HTTP method, such as GET, POST, PUT, PATCH, or DELETE.
The media type of the data, such as JavaScript Object Notation (JSON) or XML.
APIs often need to provide services for several different but related things. For example, our pizza API can manage pizzas, customers, and orders. We use routing to map URIs to logical partitions in the code to route requests to https://localhost:5000/pizza to PizzaController and to route requests to https://localhost:5000/order to OrderController.

3. The benefits of creating APIs in ASP.NET Core

With ASP.NET, the same framework and patterns can be used to generate web pages and services. This means that model classes and validation logic can be reused in the same project, and even serve web pages and services in parallel. This approach has many advantages:
Simple serialization: ASP.NET was designed to provide a modern Web experience. The endpoint will automatically serialize the class into well-formed JSON out of the box. No special configuration is required. Of course, you can customize serialization for endpoints with unique requirements.
Authentication and Authorization: For security reasons, API endpoints have built-in support for the industry-standard JSON Web Token (JWT). Policy-based authorization gives you the flexibility to define powerful access control rules in code.
Routing with code: ASP.NET lets you use attributes to define routes and predicates inline with your code. Data in the request path, query string, and request body will be automatically bound to method parameters.
HTTPS by default: HTTPS is an important part of modern professional Web APIs. It relies on end-to-end encryption to provide privacy and help ensure that API calls cannot be intercepted and altered between client and server.
ASP.NET provides out-of-the-box support for HTTPS. It automatically generates a test certificate and easily imports it to enable local HTTPS, allowing you to safely run and debug your application before publishing it.

4. Share code and knowledge with .NET applications

Use .NET skills and the ecosystem to share the logic of your Web API with other apps built using .NET, including mobile apps, web apps, desktop apps, and services

5. Use .NET HTTP REPL to test Web API

When you develop traditional websites, you typically view and test your work in a web browser. Web APIs accept and return data instead of HTML, so web browsers are not the best tools for testing Web APIs.
One of the easiest options for browsing and interacting with Web APIs is the .NET HTTP REPL. REPL stands for read-evaluate-print loop. This is a simple and common way to build an interactive command line environment.

create

Download visual studio from the official website.
Visual studio operation process under Mac

1. Open visual studio

insert image description here

2. Select Web—>API—>Next

insert image description here

3. Uncheck Configure HTTPS—>Next

insert image description here

4. Set the project name —> create

insert image description here

Introduction to Basic Project Architecture

insert image description here
You can see a simple MVC architecture at the project level as follows, here:
M indicates that WeatherForecast.cs can be managed in the model folder
V can be seen when running
insert image description hereC indicates that WeatherForecastController.cs is managed in the Controllers folder

Among them, launchSettings.json is mainly used for Html to display the message from which controller after running.
appsettings.json is some setting information of the application, which basically does not need to be changed.

Next section: Web API study notes (2) - detailed explanation of basic WebApi architecture configuration

Guess you like

Origin blog.csdn.net/weixin_45724919/article/details/126645297