Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. app.Map("/custom-prefix", api =>
  2. {
  3. api.UseMvc();
  4. });
  5.  
  6. services.AddMvc(options =>
  7. {
  8. options.Conventions.Insert(0, new RouteConvention(new RouteAttribute("custom-prefix")));
  9. });
  10.  
  11. public class ApiPrefixConvention: IApplicationModelConvention
  12. {
  13. private readonly string prefix;
  14. private readonly Func<ControllerModel, bool> controllerSelector;
  15. private readonly AttributeRouteModel onlyPrefixRoute;
  16. private readonly AttributeRouteModel fullRoute;
  17.  
  18. public ApiPrefixConvention(string prefix, Func<ControllerModel, bool> controllerSelector)
  19. {
  20. this.prefix = prefix;
  21. this.controllerSelector = controllerSelector;
  22.  
  23. // Prepare AttributeRouteModel local instances, ready to be added to the controllers
  24.  
  25. // This one is meant to be combined with existing route attributes
  26. onlyPrefixRoute = new AttributeRouteModel(new RouteAttribute(prefix));
  27.  
  28. // This one is meant to be added as the route for api controllers that do not specify any route attribute
  29. fullRoute = new AttributeRouteModel(
  30. new RouteAttribute("api/[controller]"));
  31. }
  32.  
  33. public void Apply(ApplicationModel application)
  34. {
  35. // Loop through any controller matching our selector
  36. foreach (var controller in application.Controllers.Where(controllerSelector))
  37. {
  38. // Either update existing route attributes or add a new one
  39. if (controller.Selectors.Any(x => x.AttributeRouteModel != null))
  40. {
  41. AddPrefixesToExistingRoutes(controller);
  42. }
  43. else
  44. {
  45. AddNewRoute(controller);
  46. }
  47. }
  48. }
  49.  
  50. private void AddPrefixesToExistingRoutes(ControllerModel controller)
  51. {
  52. foreach (var selectorModel in controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList())
  53. {
  54. // Merge existing route models with the api prefix
  55. var originalAttributeRoute = selectorModel.AttributeRouteModel;
  56. selectorModel.AttributeRouteModel =
  57. AttributeRouteModel.CombineAttributeRouteModel(onlyPrefixRoute, originalAttributeRoute);
  58. }
  59. }
  60.  
  61. private void AddNewRoute(ControllerModel controller)
  62. {
  63. // The controller has no route attributes, lets add a default api convention
  64. var defaultSelector = controller.Selectors.First(s => s.AttributeRouteModel == null);
  65. defaultSelector.AttributeRouteModel = fullRoute;
  66. }
  67. }
  68.  
  69. services.AddMvc(opts =>
  70. {
  71. var prefixConvention = new ApiPrefixConvention("api/", (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
  72. opts.Conventions.Insert(0, prefixConvention);
  73. });
  74.  
  75. public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder, string prefix = "api/")
  76. {
  77. // instantiate the convention with the right selector for your library.
  78. // Check for namespace, marker attribute, name pattern, whatever your prefer
  79. var prefixConvention = new ApiPrefixConvention(prefix, (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
  80.  
  81. // Insert the convention within the MVC options
  82. builder.Services.Configure<MvcOptions>(opts => opts.Conventions.Insert(0, prefixConvention));
  83.  
  84. // perform any extra setup required by your library, like registering services
  85.  
  86. // return builder so it can be chained
  87. return builder;
  88. }
  89.  
  90. services.AddMvc().AddMyLibrary("my/api/prefix/");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement