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

相关推荐
weixin_379880921 天前
.Net Core WebApi集成Swagger
java·服务器·.netcore
The Future is mine3 天前
.Net Core 在Linux系统下创建服务
linux·运维·.netcore
*长铗归来*4 天前
ASP.NET Core Web API 中控制器操作的返回类型及Swagger
后端·c#·asp.net·.netcore
IDOlaoluo4 天前
VS2017 安装 .NET Core 2.2 SDK 教程(包括 dotnet-sdk-2.2.108-win-x64.exe 安装步骤)
.netcore
csdn_aspnet12 天前
使用 Entity Framework Code First 方法创建 ASP.NET Core 5.0 Web API
.netcore·webapi
小先生81212 天前
.NET Core项目中 Serilog日志文件配置
c#·.netcore
爱吃香蕉的阿豪12 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
csdn_aspnet12 天前
ASP.NET Core 10.0 的主要变化
.netcore
csdn_aspnet15 天前
在 C# .NETCore 中使用 MongoDB(第 1 部分):驱动程序基础知识和插入文档
mongodb·.netcore
csdn_aspnet15 天前
在 C# .NETCore 中使用 MongoDB(第 3 部分):跳过、排序、限制和投影
mongodb·c#·.netcore