Last active
January 13, 2017 21:16
-
-
Save robgha01/038a0dccc2910ae90183a628f4f39fc9 to your computer and use it in GitHub Desktop.
Umbraco 7 How to create a custom route.
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
public class ApplicationRouteBootstrap : ApplicationEventHandler | |
{ | |
/// <inheritdoc /> | |
protected override void ApplicationStarted( | |
UmbracoApplicationBase umbracoApplication, | |
ApplicationContext applicationContext) | |
{ | |
RouteTable.Routes.MapUmbracoRoute( | |
"Profile", | |
"{action}/{alias}", | |
new { Controller = "ProfilePageModel", Action = "ProfilePageModel" }, | |
new ProfilePageNodeRouteHandler(), | |
new { Alias = new ProfileRouteConstraint() }); | |
} | |
} |
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
public class ProfilePageModelController : UCodeFirstController | |
{ | |
/// <inheritdoc /> | |
public override ActionResult Index(RenderModel model) | |
{ | |
try | |
{ | |
// Get strongly typed model | |
var m = model.Content.ToStrongContent<ProfilePageModel>(); | |
var alias = RouteData.Values["alias"]; // <= Here is the user we want to bind to the profile! :D | |
return View(model.Content.GetTemplateAlias(), m); | |
} | |
catch (InvalidOperationException) | |
{ | |
// Exception, return base RenderModel | |
return View(model.Content.GetTemplateAlias(), model); | |
} | |
} | |
} |
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
public class ProfileRouteConstraint : IRouteConstraint | |
{ | |
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) | |
{ | |
// This is the magic that makes umbraco only hit the right controller / Profile woooooop. | |
var controller = values["controller"]?.ToString() ?? string.Empty; | |
var controllerName = nameof(ProfilePageModel); | |
var alias = values["alias"]?.ToString() ?? string.Empty; | |
if (controller.Equals(controllerName) && !string.IsNullOrEmpty(alias)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} |
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
public class ProfilePageNodeRouteHandler : UmbracoVirtualNodeRouteHandler | |
{ | |
/// <inheritdoc /> | |
protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext) | |
{ | |
try | |
{ | |
string alias = typeof(ProfilePageModel).Name; | |
if (requestContext != null && requestContext.HttpContext.Request.Url != null) | |
{ | |
var path = requestContext.HttpContext.Request.Url.GetAbsolutePathDecoded(); | |
var parts = path.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries); | |
if (parts.Length == 2) | |
{ | |
try | |
{ | |
// Try to resolve it as a route. | |
var content = UmbracoContext.Current.ContentCache.GetByRoute(string.Join("/", parts.Take(parts.Length - 1)).EnsureStartsWith("/")); | |
if (content.DocumentTypeAlias.Equals(alias)) | |
{ | |
return content; | |
} | |
} | |
catch (Exception) | |
{ | |
// ignored | |
} | |
// Get all root nodes. | |
var rootNodes = umbracoContext.ContentCache.GetAtRoot(); | |
var profilePage = rootNodes.DescendantsOrSelf(alias).FirstOrDefault(x => x.UrlName == parts.First()); | |
if (profilePage != null) | |
{ | |
return profilePage; | |
} | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
// ignored | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment