If I'm using Attribute Routing in an ASP.NET controller, are those path templates considered hard coded? For instance, if I add [HttpGet("/employee")]
or [Route("/employee")]
, is the /employee
path a hard-coded value?

- 7,102
- 69
- 48
- 77

- 959
- 1
- 11
- 32
-
3What do you mean by hard-coding? The routes are solely based on the nomenclature used by the developer to develop an endpoint for the API to be accessed. Hard-coding is merely using a fixed static value for a logic that can potentially change based on different events. In this case, the route will never change although the inner logic can change multiple times and the client would not be aware of that. – Rahul Sharma Feb 06 '20 at 09:36
-
one of my senior said that this is hard-coding and can be avoided but I don't get the point , if in future it wont change how can we say its hard coded. So its not hard coded right ? @RahulSharma – Shubham Tiwari Feb 06 '20 at 09:39
-
Well, it solely depends on the architecture that you are following or have been asked to follow. There are ways to create your routes dynamically but that also depends on what you are developing. So, you cannot really say that this is hard coding but more of a requirement to requirement basis for different scenarios. – Rahul Sharma Feb 06 '20 at 09:45
-
It can be considered as hardcoded nothing wrong in that, but should it be replaced by Constant variable calling this value from a constant class, No for me. If it needs to be replaced by a Constant variable than one of the main coding practices i.e Readability of the code is not followed. It will not be easy for developers to understand someone else code if you use Constant variable everywhere, in this case, I have to see your constant class again and again for each constant I see. – Bippan Kumar Feb 06 '20 at 10:06
-
Hello, Shubham. Did my answer below address your question? If so, can I ask that you mark it as the answer? And, if not, is there anything we can help clarify? – Jeremy Caney Mar 17 '20 at 05:38
-
Hello @JeremyCaney your answer did resolve a few of the queries that I had but however your explanation about the "Distributable Class Library" was not very clear to me can you give me a easier explanation as to where it can be advantageous ? Thank you ! – Shubham Tiwari Mar 17 '20 at 09:05
-
1Ah, yes! I’ll update my answer with more details in my morning, but the quick summary is that you might have a web application that can be distributed as an assembly—possibly even via NuGet—and then embedded into multiple different web applications. For example, I have a CMS editor that’s distributed this way. Then, each site can reuse the same controllers and views without having to maintain their own copies of the code. For one approach to this, take a look at [Razor Class Libraries](https://docs.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.1&tabs=visual-studio). – Jeremy Caney Mar 17 '20 at 09:35
1 Answers
As others have noted in the comments, these routes are definitely hard-coded—but that may not be a real concern. It would be useful to better understand what requirements are driving this concern, as the solution will ultimately depend on that. In the meanwhile, I can provide some high-level guidance based on common scenarios.
Single Tier Web Application
If your controllers are distributed as part of your web application, and your routes are always defined at design time (i.e., as part of your development process), then it's just a stylistic preference as to whether your route paths are hard-coded as part of your controllers or hard-coded as part of your Startup
class. This is the typical scenario for most web applications.
Distributable Class Library
If you're distributing your controllers as part of a Class Library (or a Razor Class Library) which is intended to be used on multiple web applications, then this is a bigger consideration. In that scenario, hard-coding the routes as part of your controller prevents consumers of your class library from modifying the paths.
This could be advantageous if you want to ensure the same routes are always used while simultaneously eliminating the need to configure them on a per-application basis. But it may instead make sense to allow each application to customize these routes in their Startup
class, thus giving consumers flexibility to customize where your library's endpoints live. If that's the case, hard-coding these values in your class library's controllers is undesirable.
Extension Methods
If the latter is the requirement, it's common to include an extension method for
IRouteBuilder
and/orIEndpointRouteBuilder
so that consumers can easily configure routes for your library, while accepting parameters for any variables you expect them to override, such as a path prefix. This provides a balance between flexibility and ease of configuration. For example, this might look something like the following:public static ControllerActionEndpointConventionBuilder MapEmployeeRoute( this IEndpointRouteBuilder routes, string pathPrefix = "Employee", string controller = "Employee", string action = "Index" ) => routes.MapControllerRoute( name: "Employee-" + pathPrefix?.Replace("/", "", System.StringComparison.OrdinalIgnoreCase), pattern: pathPrefix?.Trim('/') + "/{action}", defaults: new { controller, action, pathPrefix } );
Which could be used with any of the following calls:
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) > { … app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapEmployeeRoute(); //Maps /Employee endpoints.MapEmployeeRoute("Administration/Staff"); //Maps /Administration/Staff/ endpoints.MapEmployeeRoute("/Administration/Staff/"); //Tolerate variations endpoints.MapEmployeeRoute("Staff", "MyEmployee"); //Reference derived controller endpoints.MapEmployeeRoute("Staff", "Employee", "Add"); //Default to the Add() action }); }
Dynamic Route Configuration
Note: This is a pretty uncommon scenario, and so I hesitate to even bring it up. That said, I'm including it for completeness in case it relates to your requirements.
Another scenario where hard-coding the routes may be a concern is if, for some reason, you need them to be dynamically configured at run-time and/or are using configuration values from an external data source.
For example, if you're building a platform as a service (PAAS), it's sometimes desirable to provide a web interface where users can configure routes associated with particular services, and then have those dynamically registered, instead of hard-coding them into the platform itself. That's a far more involved approach, though.

- 7,102
- 69
- 48
- 77