.net 6.0 - ControllerBase not working with Minimal Api
Hi,
I'm expecting the below code to show this result:
instead i'm getting:
http://localhost:5000/test1 => ok
http://localhost:5000/test2 => 404 - Not Found
The same code will work fine within a regular webapi project.
Only with linqpad, it appears dotnet isn't picking up the ControllerBase classes.
Code:
`
async Task Main() { var builder = WebApplication.CreateBuilder(); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(); app.MapControllers(); app.MapGet("test1", () => "ok"); app.Run(); } [ApiController, Route("test2")] public class MyController : ControllerBase { [HttpGet] public string Test2() => "ok"; }
`
Note: the issue is only with the minimal api builder i.e. WebApplication.CreateBuilder
Comments
Check this thread: https://forum.linqpad.net/discussion/2599/same-mini-asp-net-core-code-in-linqpad-unable-to-get-responsed-but-good-in-visual-studio
builder.Services.AddControllers().AddApplicationPart(this.GetType().Assembly);
http://share.linqpad.net/oa2oex.linq