In this tutorial, you will learn how to enable attribute routing within Umbraco V8. Attribute routing is useful if you want to create a custom API within Umbraco and you want to have more control over the URL that the front-end will use to access it. Be default any API that uses inherits from UmbracoApiController will need to follow this URL structure:
To enable attribute routing you will need to turn on this feature when Umbraco boots-up. Running custom configuration code on Umbraco boot-up is done using something called a component. A component can be created to run any custom code that you desire. A component will also need to be registered using a composer. If you are new to components or composers then you can learn more about them here. The code required to build a component that will enable attribute routing will look like this:
The composer to register this component would look like this:
With both of these things added to your project, attribute routing will now work! In a controller you can then use the route attribute feature to customise your API URL like this:
Happy coding!