Is it possible to use Web API within a .NET Core application that is not using MVC, but making use of app.Run?
I want to provide a simple API and would rather use Web API than roll my own. I've added in MvcCore in ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddMvcCore(x =>
{
x.RespectBrowserAcceptHeader = true;
}).AddFormatterMappings()
.AddJsonFormatters();
// ...
}
I added a very simple Controller:
[Route("/api/test")]
public class TestController : Controller
{
[HttpGet]
[Route("")]
public async Task<IActionResult> Get()
{
return Ok("Hello World");
}
}
However, when I attempt to hit /api/test my normal processing code within App.Run processes.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var scopeFactory = _serviceProvider.GetRequiredService<IServiceScopeFactory>();
// ...
app.Run(async (context) =>
{
using (var scope = scopeFactory.CreateScope())
{
var request = new Request(context);
var response = new Response(context);
await scope.ServiceProvider.GetRequiredService<IRequestFactory>().ProcessAsync(request, response);
}
});
}
How do I defer to the Web API if I don't want to handle the request?