当前位置: 首页 > news >正文

Dify创建自定义工具,调用ASP.NET Core WebAPI时的注意事项(出现错误:Reached maximum retries (3) for URL ...)

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();// 配置Swagger
builder.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")}});// 定义Schemac.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、端口规则,一定要加。

结果:

好了,现在有手有脚了。


http://www.mrgr.cn/news/34640.html

相关文章:

  • 微搭低代码入门03函数
  • 第8章利用CSS制作导航菜单
  • 关于Unity使用LookAt时为什么不能旋转
  • 1111111111待修改--大流量分析(三)-BUUCTF
  • CSS3 用户界面
  • 【深度学习目标检测|YOLO算法5-2-1】YOLO家族进化史:从YOLOv1到YOLOv11的架构创新、性能优化与行业应用全解析...
  • python爬虫案例——抓取链家租房信息
  • JSON合并工具
  • 苍穹外卖学习笔记(九)
  • 微信抢红包设计
  • Vue开发前端图片上传给java后端
  • 19_Python中的上下文管理器
  • QT中添加资源文件(一看就会)
  • Linux常用命令
  • 开关频率与谐振频率对应的模态图
  • 一行命令,一分钟轻松搞定SSL证书自动续期
  • 类中的特殊内容
  • 华为高级交换技术笔记 2024-2025
  • 计算机组成原理==初识二进制运算
  • 研究生三年概括
  • Elasticsearch案例
  • Python中的“打开与关闭文件”:从入门到精通
  • X86架构(八)——32位处理器架构
  • 意得辑(Editage)润色全网最低折扣
  • JIT(Just-In-Time Compilation)编译器及其原理实现
  • 浮动静态路由