Skip to content

emoa2l/RouteDecorator

Repository files navigation

Simple class to let you decorate your methods in the controller with route attributes rather than store all the named routes in global

RouteDecorator example setup

//App_Start.RouteDecorator.cs
public class RouteDecorator
    {

        public static void Start()
        {
            RouteDecorator.Init();
        }

        public static void Init()
        {                        
            foreach (var attr in from type in Assembly.GetExecutingAssembly().GetTypes() 
                                 select type.GetMethods()
                                 .Where(y => Attribute.IsDefined(y, typeof(RouteDecoratorAttribute))).ToList() 
                                 into methods from method in methods 
                                 select method.GetCustomAttributes().OfType<RouteDecoratorAttribute>().ToArray() 
                                 into attributes from attr in attributes 
                                 select attr)
            {
                RouteTable.Routes.MapRoute(
                    name: attr.Name,
                    url: attr.Route,
                    defaults: new { controller = attr.Controller, action = attr.Action }
                    );
            }
        }
    }

//Some Controller
[RouteDecorator("some/sample/{itemId}", Action = "Some", Controller = "Sample", Name = "SampleItem")]
public ActionResult Sample(string itemId)
{
    return View();
}

About

MVC Declarative Route Decorator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages