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

相关推荐
武藤一雄17 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
武藤一雄2 天前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
van久6 天前
.Net Core 学习:Razor Pages中 HTML 表头字段的两种写法对比
学习·html·.netcore
武藤一雄6 天前
C# 万字拆解线程间通讯?
后端·微软·c#·.net·.netcore·多线程
武藤一雄7 天前
.NET中到底什么是SignalR (持续更新)
后端·微软·c#·asp.net·.net·.netcore·signalr
by__csdn7 天前
第二章 (.NET Core环境搭建)第二节( Visual Studio Code)
ide·vscode·c#·vue·asp.net·.net·.netcore
by__csdn7 天前
第二章 (.NET Core环境搭建)第三节( Visual Studio for Mac)
ide·kubernetes·c#·asp.net·.net·.netcore·visual studio
武藤一雄8 天前
C#:进程/线程/多线程/AppDomain详解
后端·微软·c#·asp.net·.net·wpf·.netcore
武藤一雄9 天前
C#:Linq大赏
windows·后端·microsoft·c#·.net·.netcore·linq
by__csdn9 天前
第一章 (ASP.NET Core入门)第三节( 认识.NET Standard)
后端·c#·asp.net·.net·.netcore·f#·vb.net