The ApiController attribute in .NET Core is used to mark a class as an API controller. API controllers are classes that handle HTTP requests and responses. They are used to create RESTful APIs.
The ApiController attribute has the following properties:
RoutePrefix: The route prefix for the controller. This is the part of the URL that will be used to access the controller.
ApiVersion: The API version for the controller. This is used to indicate the version of the API that the controller supports.
AllowAnonymous: Whether or not anonymous users are allowed to access the controller.
The following is an example of a class that is marked with the ApiController attribute:
C#
[ApiController]
[RoutePrefix("/products")]
public class ProductController : ControllerBase {
// ...
}
This class is an API controller that can be accessed at the /products endpoint. Anonymous users are allowed to access this controller.
Here are some additional tips for using the ApiController attribute:
Use the ApiController attribute on all classes that are API controllers. This will help to ensure that your code is organized and easy to maintain.
Use the RoutePrefix property to specify the route prefix for each controller. This will help to keep your URLs organized and easy to understand.
Use the ApiVersion property to specify the API version for each controller. This will help to ensure that your API is backward compatible.
Use the AllowAnonymous property to control whether or not anonymous users are allowed to access each controller.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
ApiControllerattribute in .NET Core is used to mark a class as an API controller. API controllers are classes that handle HTTP requests and responses. They are used to create RESTful APIs.The
ApiControllerattribute has the following properties:RoutePrefix: The route prefix for the controller. This is the part of the URL that will be used to access the controller.ApiVersion: The API version for the controller. This is used to indicate the version of the API that the controller supports.AllowAnonymous: Whether or not anonymous users are allowed to access the controller.The following is an example of a class that is marked with the
ApiControllerattribute:C#
This class is an API controller that can be accessed at the
/productsendpoint. Anonymous users are allowed to access this controller.Here are some additional tips for using the
ApiControllerattribute:ApiControllerattribute on all classes that are API controllers. This will help to ensure that your code is organized and easy to maintain.RoutePrefixproperty to specify the route prefix for each controller. This will help to keep your URLs organized and easy to understand.ApiVersionproperty to specify the API version for each controller. This will help to ensure that your API is backward compatible.AllowAnonymousproperty to control whether or not anonymous users are allowed to access each controller.