.NET Core Web API 控制台输出显示方法
.NET Core Web API 控制台输出显示方法
要在 .NET Core Web API 中显示控制台输出,你可以使用 'Console.WriteLine' 方法。你可以在控制器的操作方法中使用它,也可以在中间件中使用它。
在控制器中显示输出
[ApiController]
[Route('api/[controller]')]
public class MyController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
Console.WriteLine('This will be displayed in the console');
return Ok();
}
}
在中间件中显示输出
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine('This will be displayed in the console');
await _next(context);
}
}
请注意,控制台输出将在应用程序的控制台窗口中显示,而不是在浏览器中。
原文地址: https://www.cveoy.top/t/topic/na8g 著作权归作者所有。请勿转载和采集!