简体   繁体   English

使用属性路由定义的路径模板是否被视为硬编码?

[英]Are path templates defined using Attribute Routing considered hard coded?

If I'm using Attribute Routing in an ASP.NET controller, are those path templates considered hard coded?如果我在 ASP.NET 控制器中使用属性路由,这些路径模板是否被视为硬编码? For instance, if I add [HttpGet("/employee")] or [Route("/employee")] , is the /employee path a hard-coded value?例如,如果我添加[HttpGet("/employee")][Route("/employee")]/employee路径是硬编码值吗?

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单层 Web 应用程序

If your controllers are distributed as part of your web application, and your routes are always defined at design time (ie, 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.如果您的控制器作为 Web 应用程序的一部分分发,并且您的路由始终在设计时定义(即,作为开发过程的一部分),那么关于您的路由路径是否被硬编码为一部分,这只是一种风格偏好您的控制器或硬编码为您的Startup类的一部分。 This is the typical scenario for most web applications .这是大多数 Web 应用程序的典型场景

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.如果您将控制器作为旨在用于多个Web 应用程序的类库(或 Razor 类库)的一部分进行分发,那么这是一个更大的考虑因素。 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.但是,允许每个应用程序在它们的Startup类中自定义这些路由可能更有意义,从而使消费者可以灵活地自定义库的端点所在的位置。 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/or IEndpointRouteBuilder 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.如果需要后者,通常会包含IRouteBuilder和/或IEndpointRouteBuilder的扩展方法,以便消费者可以轻松地为您的库配置路由,同时接受您希望它们覆盖的任何变量的参数,例如路径前缀。 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.例如,如果您正在构建一个平台即服务 (PAAS),有时需要提供一个 Web 界面,用户可以在其中配置与特定服务关联的路由,然后动态注册这些路由,而不是将它们硬编码到平台本身。 That's a far more involved approach, though.不过,这是一种更复杂的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM