Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reference OpenAPI service in ASP.NET Core

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to quote OpenAPI services in ASP.NET Core", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to quote OpenAPI services in ASP.NET Core" article.

First, introduce Swagger into WebAPI project

To introduce Swagger into the ASP.NET Core project, you can use Swashbuckle and ASP.NET Core to get started with this official Microsoft document.

Create an ASP.NET Core Web API service

Execute the following command to introduce the corresponding package:

Install-Package Swashbuckle.AspNetCore-Version 5.6.3

Add the Swagger generator to the service collection in the Startup.ConfigureServices method:

Public void ConfigureServices (IServiceCollection services) {services.AddControllers (); services.AddSwaggerGen ();}

In the Startup.Configure method, enable middleware to service the generated JSON documents and Swagger UI:

Public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {app.UseSwagger (); app.UseSwaggerUI (c = > c.SwaggerEndpoint ("/ swagger/v1/swagger.json", "My API V1")); app.UseRouting (); app.UseEndpoints (endpoints = > endpoints.MapControllers ());}

Start the project to access the Swagger page after configuration is complete

Click / swagger/v1/swagger.json on the page to jump to our URL

For example, here is: http://localhost:5000/swagger/v1/swagger.json

2. Add OpenAPI service reference

Create another project in the solution, the .NET Core project, and what I'm creating here is the console project.

Right-click the dependency in the project and select the service to which the connection is added

Click the add button in the service reference (OpenAPI, gRPC) in the new tab that appears.

Then select OpenAPI and click next.

Restart the WebAPI project in step 1

Select URL and fill in the previous Swagger description file URL: http://localhost:5000/swagger/v1/swagger.json, and click finish to wait for completion.

(if you download swagger.json, it is also feasible to use files here.)

There is an extra ~ / OpenAPIs/swagger.json file in the project after it is added.

You can then call the interface directly in the code:

Static async Task Main (string [] args) {var http = new HttpClient (); var client = new swaggerClient ("http://localhost:5000/", http); var result = await client.WeatherForecastAsync (); foreach (var item in result) {Console.WriteLine ($" {item.Date}, {item.TemperatureF}, {item.Summary} ") } Console.WriteLine ("Hello World!");}

For configured service references that need to be updated because they have changed, you can simply refresh them in more lists.

The above is about the content of this article on "how to quote OpenAPI Services in ASP.NET Core". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report