Implementing MVC attribute routing into Umbraco 7 is pretty straight forward. You might want to consider using this approach if you create your own custom MVC controllers for Umbraco.
When an Umbraco application starts up, classes inheriting from interface IApplicationEventHandler are automatically consumed and events called when appropriate. The class below implements this interface and has code for the OnApplicationStarted method call. This class can be placed in your App_Start folder together with other start-up related classes.
using System.Web.Mvc; using System.Web.Routing; using Umbraco.Core; namespace Website.App_Code { /// <summary> /// Website Startup Handler /// </summary> public class WebsiteStartupHandler: IApplicationEventHandler { /// <summary> /// On Application Started /// </summary> /// <param name="umbracoApplication">Umbraco Application</param> /// <param name="applicationContext">Application Context</param> public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { RouteTable.Routes.MapMvcAttributeRoutes(); } public void OnApplicationInitialized( UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { } public void OnApplicationStarting( UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { } } }
The code within this method calls the standard MVC5+ function to crawl your controllers and create routes for any methods annotated with a route attribute. For example:-
/// <summary> /// Get Contact View /// </summary> /// <returns>Contact View</returns> [HttpGet] [Route("contact", Name = "Contact")] public ActionResult Contact()
That's about it for implementing attribute routing for MVC and Umbraco - please feel free to leave me your comments!
Leave Your Comments...