I am creating an asp.net web api project, I am storing my data in mssql server db, and I am using an Entity Framework. I was following the microsoft guide (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio), it uses an api controller with entity framework scaffold item. In other guides I found people using controllers derived from ApiController class and the routing there is made via web.config file. What is the actual difference, and what should I use? I suppose I should follow the MC guide cause it is fairly new (was added a week ago).
P.S.: right now, I am not planning to add "view" part to my app, first I want to create all the API calls, but later I whould like to.
CodePudding user response:
Asp.net6 brought new ways of working with controllers, detaching a little from the MVC pattern we had, but this model of yours, if you are following the tutorial, ends up inheriting from the MVC standard ApiController, look at this part of the official tutorial:
Marks the class with the [ApiController] attribute. This attribute indicates that the controller responds to web API requests. For information about specific behaviors that the attribute enables, see Create web APIs with ASP.NET Core. Uses DI to inject the database context (TodoContext) into the controller. The database context is used in each of the CRUD methods in the controller. The ASP.NET Core templates for:
Controllers with views include [action] in the route template. API controllers don't include [action] in the route template.
well this implies that your controller is being mapped into the aspnet mvc stream and being registered there when you call app.MapControllers(); in your Program.cs
and it's not necessary to configure anything in webConfig, which doesn't even exist anymore since the framework
You can follow the tutorial and everything will be fine.