I'm currently building an ASP.NET Core app, and up until now haven't used controllers since everything was managed by a SignalR hub. I have decided to add an API controller (with the attribute) in order to handle API requests from the client, such as fetching from the database. Here is my API controller:
[ApiController]
[Route("api/[controller]")]
public class BaseApiController : ControllerBase
{
private IMediator _mediator;
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
}
And here is the Messages controller which derives from API controller:
public class MessagesController : BaseApiController
{
private readonly DataContext _context;
public MessagesController(DataContext dataContext)
{
_context = dataContext;
}
[HttpGet]
public async Task<ActionResult<List<Message>>> GetMessages() =>
await Mediator.Send(new List.Query());
}
For some reason, when sending a request from the client side I get a 404. I think, that it happens because of some sort of "clash" between the API and the IIS launch. Sorry if this may sound stupid, I'm new.
If launching the project with an API controller via IIS isn't a problem, then I'll look elsewhere.
CodePudding user response:
In Startup.cs you need to add the following code to ConfigureServices()
services.AddControllers();
and to Configure(IApplicationBuilder app)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
...
});