Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created March 14, 2013 17:42
Show Gist options
  • Save mikeobrien/5163441 to your computer and use it in GitHub Desktop.
Save mikeobrien/5163441 to your computer and use it in GitHub Desktop.
[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