用.Net Core框架创建一个Web API接口服务器

  1. 我们选择一个Web Api类型的项目创建一个解决方案
  2. 为解决方案取一个名称
  3. 我们这里选择的是。Net 8.0框架

注意,需要勾选的项。

  • 我们找到appsetting.json配置文件
  • appsettings.json配置文件内容如下
javascript 复制代码
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Enable": false,
        "Url": "http://*:6565"
      }
    }
  },
  "AllowedHosts": "*",
  "ConnectionString": "User Id=root;Password=123456;Host=localhost;Database=base;charset=utf8;connection type=mysql"
}

运行之后,直接在浏览器中访问【http://localhost:6565/swagger/index.html】即可。

我们在Program中可以配置如下内容,更Swagger的标题名称

如果我们想添加一个新的控制器

  • HomeController.cs代码如下
cs 复制代码
[ApiController, Route("api/home")]
public class HomeController : ControllerBase
{
    /// <summary>
    /// 测试接口
    /// </summary>
    /// <returns></returns>
    [HttpGet(), Route("test")]
    public ActionResult Test()
    {
        var _data = new
        {
            Result = true,
            CurrPos = 0.00,
            Data = new object()
        };
        return Ok(new JsonResult(_data));
    }
}

这样我们就可以看到新的控制器接口

相关源代码【GitCode - 全球开发者的开源社区,开源代码托管平台

相关推荐
爱吃香蕉的阿豪3 天前
乐思 AI 智能识别平台(基于 YOLO,.NET+Vue3 开发)开源指南
人工智能·yolo·开源·aigc·.netcore
时光追逐者3 天前
C#/.NET/.NET Core优秀项目和框架2025年7月简报
c#·.net·.netcore
步、步、为营8 天前
.NET Core 3.1 升级到 .NET 8
microsoft·.net·.netcore
时光追逐者10 天前
C#/.NET/.NET Core技术前沿周刊 | 第 48 期(2025年7.21-7.27)
c#·.net·.netcore·.net core
爱吃香蕉的阿豪10 天前
SignalR 全解析:核心原理、适用场景与 Vue + .NET Core 实战
vue.js·microsoft·c#·.netcore·signalr
工藤新一OL12 天前
把xml的格式从utf-8-bom转为utf-8
xml·c#·asp.net·.netcore·visual studio
王柏龙12 天前
Asp.net core mvc中TagHelper的GetChildContentAsync和Content区别
mvc·.netcore
编程乐趣14 天前
基于.Net Core开源的库存订单管理系统
.netcore
lgaof65822@gmail.com17 天前
ASP.NET Core Web API 中集成 DeveloperSharp.RabbitMQ
后端·rabbitmq·asp.net·.netcore
爱吃香蕉的阿豪17 天前
在.NET Core API 微服务中使用 gRPC:从通信模式到场景选型
微服务·.netcore·信息与通信·grpc