forked from EduardoPires/EquinoxProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
095ce0e
commit 40501a9
Showing
10 changed files
with
218 additions
and
831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.Routing; | ||
|
||
namespace Equinox.WebApi.Configurations | ||
{ | ||
public class RouteConvention : IApplicationModelConvention | ||
{ | ||
private readonly AttributeRouteModel _centralPrefix; | ||
|
||
public RouteConvention(IRouteTemplateProvider routeTemplateProvider) | ||
{ | ||
_centralPrefix = new AttributeRouteModel(routeTemplateProvider); | ||
} | ||
|
||
public void Apply(ApplicationModel application) | ||
{ | ||
foreach (var controller in application.Controllers) | ||
{ | ||
var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList(); | ||
if (matchedSelectors.Any()) | ||
{ | ||
foreach (var selectorModel in matchedSelectors) | ||
{ | ||
selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix, | ||
selectorModel.AttributeRouteModel); | ||
} | ||
} | ||
|
||
var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList(); | ||
|
||
if (unmatchedSelectors.Any()) | ||
{ | ||
foreach (var selectorModel in unmatchedSelectors) | ||
{ | ||
selectorModel.AttributeRouteModel = _centralPrefix; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class MvcOptionsExtensions | ||
{ | ||
public static void UseCentralRoutePrefix(this MvcOptions opts, IRouteTemplateProvider routeAttribute) | ||
{ | ||
opts.Conventions.Insert(0, new RouteConvention(routeAttribute)); | ||
} | ||
} | ||
} |
Oops, something went wrong.