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

相关推荐
睡前要喝豆奶粉9 小时前
多表分页联查——EF Core方式和Dapper方式
c#·.netcore
csdn_aspnet18 小时前
.NETCore、.NET 7 和 RabbitMQ 的发布-订阅模式
rabbitmq·.netcore·.net7.
爱吃香蕉的阿豪19 小时前
深入理解 .NET Core 中的 IServiceScopeFactory:用法、场景与静态类依赖注入
.netcore
sky-stars1 天前
.NET 泛型编程(泛型类、泛型方法、泛型接口、泛型委托、泛型约束)
c#·.net·.netcore
The Sheep 20232 天前
.NetCoreMVC 开发网页使用sass
.netcore·sass
宝桥南山3 天前
.NET10 - 尝试一下Blazor Web Assembly Standalone App的fingerprint新特性
microsoft·微软·c#·asp.net·.net·.netcore
刚子编程5 天前
ASP.NET Core Blazor 核心功能一:Blazor依赖注入与状态管理指南
开发语言·.netcore·blazor
是萝卜干呀5 天前
Backend - HTTP请求的常用返回类型(asp .net core MVC)
http·c#·.netcore·iactionresult
精神小伙就是猛5 天前
.Net Core基于EasyCore.EventBus实现事件总线
微服务·.netcore
sky-stars7 天前
Visual Studio 2022 安装使用:Entity Framework Core
asp.net·.netcore·visual studio