用.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 - 全球开发者的开源社区,开源代码托管平台

相关推荐
罗马苏丹默罕默德2 小时前
Ubuntu下部署.NetCore WebApi的方法
数据库·ubuntu·.netcore
lingxiao168882 天前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
老龄程序员2 天前
基于OpenIddict6.4.0搭建的授权UI管理界面
.netcore
武藤一雄3 天前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
冰茶_3 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
武藤一雄3 天前
C# 关于GC垃圾回收需要注意的问题(持续更新)
后端·微软·c#·.net·.netcore
武藤一雄3 天前
C# 关于应用程序域(AppDomain)需要注意的问题(持续更新)
后端·microsoft·微软·c#·.net·.netcore
csdn_aspnet8 天前
在 ASP.NET Core 中发布 RabbitMQ 消息
rabbitmq·.netcore
csdn_aspnet8 天前
在 ASP.NET Core 中使用(消费) RabbitMQ 消息
rabbitmq·.netcore
csdn_aspnet9 天前
ASP.NET Core 应用程序中的单点登录 (SSO)
.netcore·sso