ASP.NET Core Minimal API

使用 Minimal API 替代 Owin

简介

在 .NET Framework 中或者 .NET Core 6 以前,在控制台或其他桌面应用程序中使用 WebApi,可以使用 Owin 来创建,但是在 .NET Core 6 以后的版本 Owin 则不再被支持,而是引入了轻量级的开发模式 Minimal API

代码

Owin

参考 使用 OWIN Self-Host ASP.NET Web API

  • NuGet 引用官方 Microsoft.AspNet.WebApi.OwinSelfHost 库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Program
{
static void Main()
{
const string url = "http://localhost:5050";

using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"服务已启动,访问 {url}/api/test");
Console.ReadLine();
}
}
}

public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
app.UseWebApi(config);
}
}

public class TestController : ApiController
{
public string Get() => "Hello from OWIN Self-Host!";
}

Minimal API

参考 最小 API 快速参考

  • NuGet 引用官方 Microsoft.AspNet.WebApi.Client 库。
  • NuGet 引用官方 Microsoft.AspNetCore.Authentication.JwtBearer 库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
internal class Program
{
static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// 添加 MVC 服务
builder.Services.AddControllers();

// 日志只记录 Warning 及以上
builder.Logging.SetMinimumLevel(LogLevel.Warning);

builder.WebHost.UseUrls("http://localhost:5050");
var app = builder.Build();

// 可以选择使用极简的方式 URL 路径与处理逻辑绑定
// app.MapGet("/", () => "Hello from Minimal API!");

// 映射 Controller 路由
app.MapControllers();

// 启动 API
var apiTask = app.RunAsync();
Console.ReadLine();
}
}

[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public ActionResult<string> GetTest()
{
return Ok("Hello from Minimal API!");
}
}