Home > front end >  Best way to store API routes
Best way to store API routes

Time:11-28

I have an application with the web API and client library for this API. I also added a shared library, which contains the API endpoints routes.

Here is an example how it looks like:

public static class Routes
{
    /// <summary>
    /// Default Uri separator.
    /// </summary>
    private const string Slash = "/";

    /// <summary>
    /// Default API endpoint prefix.
    /// </summary>
    private const string Prefix = $"api{Slash}";

    /// <summary>
    /// API version.
    /// </summary>
    private const string Version = $"v1{Slash}";

    /// <summary>
    /// Base API route.
    /// </summary>
    private const string Base = $"{Prefix}{Version}";

    /// <summary>
    /// Static class that contains users API endpoint routes.
    /// </summary>
    public static class Users
    {
        /// <summary>
        /// Base controller route.
        /// </summary>
        private const string Controller = $"{Base}{nameof(Users)}{Slash}";

        /// <summary>
        /// Users list endpoint route.
        /// </summary>
        public const string List = $"{Controller}{nameof(List)}";

        /// <summary>
        /// User registration endpoint route.
        /// </summary>
        public const string Register = $"{Controller}{nameof(Register)}";
    }
}

Note: this is C# 10 with the new Constant Interpolated Strings feature.

Storing routes this way I can easily use them both in the API controllers declarations and within the API calls to these endpoints. But I am curious, maybe there is a better way to do this?

How would you implement shared API routes functionality? Thanks in advance.

CodePudding user response:

Using constants for API routes is the standard way. I dont think there's a better way than this. Just a small note - you are using nameof() in the Users class, which can be fragile if someone changes the class name. Do you really want that renaming the Users class will result in changing your routes ?

You can't use configurations as configuration values are read in runtime while the Route() attribute value (as all attributes) must be populated in compile time.

CodePudding user response:

As you know Web API routing is similar to ASP.Net MVC routing. Web API supprots two types of routing.

  1. Convention-based routing
  2. Attributerouting.

you can configure multiple routes in web api.

Example -

public static class WebApiConfig
{
                public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    
                // school route
        config.Routes.MapHttpRoute(
            name: "School",
            routeTemplate: "api/myschool/{id}",
            defaults: new { controller="school", id = RouteParameter.Optional }
            constraints: new { id ="/d " }
        );

                // default route
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

And you can also use Attribute routing which is best to add multiple rouing in your application. You can control over your method and add prefix as well through attribute routing.

Example :

[RoutePrefix("api/")] 

[Route("api/{employees}/{id}")]  
public Employee GetDetails(int id)  
{  
    return listEmp.First(e => e.ID == id);  
}
  • Related