LindDotNetCore ~ Add a route prefix and a link prefix
Back to directory
The routing prefix is what we call the api in api/values. Here, the api can be represented by characters with other specific meanings, such as Shop, Order, and Game. It can represent modules, this is generally in a single architecture; it can also be a small service, which is generally in the microservice architecture.
In the microservice-based design philosophy, each of our modules has become a website and a service. Their routes should be readable, not all of them are api/v1 and api/v2, we 'd better add it to the Central Daily portal. By default, this prefix is implemented by adding features to the Controller. For example:
[Produces("application/json")][Route("api/v1/EF")]public class EFController : Controller
If you want to set a uniform prefix, you need to add your own extension method, such as adding your own route prefix in the IApplicationModelConvention set. net core startup class to add extension injection, of course, the premise is that you need to design extension methods to implement insert operations on this set.
/// <Summary> /// route extension /// </summary> public static class RoutePrefixExtensions {// <summary> /// Add a custom prefix /// </summary> /// <param name = "opts"> </param> // <param name = "routeAttribute"> </param> public static void UseCentralRoutePrefix (this MvcOptions opts, IRouteTemplateProvider routeAttribute) {opts. conventions. insert (0, new RouteConvention (routeAttribute ));}}
Register this method in startup.
services.AddMvc(opt => { opt.UseCentralRoutePrefix(new RouteAttribute("Api")); });
In fact, we can add many extensions according to our rules. When AddMvc is registered, it is also registered!
Thank you for reading this article!
Back to directory