1、要配置Swagger
using Microsoft.AspNetCore.Mvc;using Microsoft.OpenApi.Models;var builder = WebApplication.CreateBuilder(args);builder.Services.AddCors(options =>{ options.AddPolicy("AllowSpecificOrigin", builder => builder.WithOrigins("http://172.20.10.7:5619") // 替换为你的Dify域名 .AllowAnyMethod() .AllowAnyHeader());});// 添加控制器服务builder.Services.AddControllers();// 配置Swaggerbuilder.Services.AddSwaggerGen(c =>{ c.SwaggerDoc("v1", new OpenApiInfo { Title = "PowerApi", Version = "v1", Description = "A simple API to calculate powers of a number from 0 to 50.", Contact = new OpenApiContact { Name = "Your Name", Email = "your.email@example.com", Url = new System.Uri("https://example.com") } }); // 定义Schema c.MapType<Dictionary<int, double>>(() => new OpenApiSchema { Type = "object", AdditionalProperties = new OpenApiSchema { Type = "number" } });});var app = builder.Build();if (app.Environment.IsDevelopment()){ app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "PowerApi v1"); c.DocumentTitle = "PowerApi Documentation"; });}app.UseHttpsRedirection();app.UseStaticFiles();app.UseCookiePolicy();app.Use(async (context, next) =>{ context.Response.Headers.Add("Content-Security-Policy", "script-src 'self';"); await next();});app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{ endpoints.MapControllers();});app.Run();
注意:IP地址的部分一定要写对了!不能是localhost或者127.0.0.1
2、launchSettings.json中
{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:1393", "sslPort": 44372 } }, "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://172.20.10.7:5169", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://172.20.10.7:7007;http://172.20.10.7:5169", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }}
注意:IP地址的部分一定要写对了!不能是localhost或者127.0.0.1
using Microsoft.AspNetCore.Mvc;using System;using System.Collections.Generic;namespace PowerApi.Controllers{ [ApiController] [Route("api/[controller]")] public class PowerController : ControllerBase { [HttpGet("{number}")] public IActionResult GetPowers(double number) { try { var powers = new Dictionary<int, double>(); for (int i = 0; i <= 50; i++) { powers[i] = Math.Pow(number, i); } // 返回JSON格式的幂运算结果 return Ok(new { powers = powers }); } catch (Exception ex) { return StatusCode(500, $"Internal server error: {ex.Message}"); } } }}
3、符合OpenAPI Swagger规范的Schema:
{ "openapi": "3.1.0", "info": { "title": "Get weather data", "description": "Retrieves current weather data for a location.", "version": "v1.0.0" }, "servers": [ { "url": "http://172.20.10.7:5169" } ], "paths": { "/api/folder": { "get": { "description": "Get temperature for a specific location", "operationId": "GetCurrentWeather", "deprecated": false } } }, "components": { "schemas": {} } }
注意:IP地址的部分一定要写对了!不能是localhost或者127.0.0.1!
4、端口规则,一定要加。
结果:
好了,现在有手有脚了。