Created
March 14, 2013 17:42
-
-
Save mikeobrien/5163441 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[AttributeUsage(AttributeTargets.Method)] | |
public class UrlAliasAttribute : Attribute | |
{ | |
public UrlAliasAttribute(params string[] urls) | |
{ | |
Urls = urls; | |
} | |
public string[] Urls { get; private set; } | |
} | |
[ConfigurationType(ConfigurationType.ModifyRoutes)] | |
public class UrlAliasConvention : IConfigurationAction | |
{ | |
public void Configure(BehaviorGraph graph) | |
{ | |
graph.Actions() | |
.Where(x => x.Method.HasAttribute<UrlAliasAttribute>()) | |
.SelectMany(x => x.Method.GetAttribute<UrlAliasAttribute>().Urls.Select(y => new { Url = y, Action = x })) | |
.OrderByDescending(x => x.Url).ToList() | |
.ForEach(x => | |
{ | |
var chain = new BehaviorChain(); | |
var call = new ActionCall(x.Action.HandlerType, x.Action.Method); | |
chain.AddToEnd(call); | |
chain.Route = call.BuildRouteForPattern(x.Url); | |
x.Action.ParentChain().Route.AllowedHttpMethods.ForEach(y => chain.Route.AddHttpMethodConstraint(y)); | |
graph.AddChain(chain); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment