LindDotNetCore ~ Add a route prefix and a link prefix

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.