Skip to content

Instantly share code, notes, and snippets.

@carlwoodhouse
Last active August 12, 2016 20:17
Show Gist options
  • Save carlwoodhouse/0108ae1166358d3f1131035fef020fce to your computer and use it in GitHub Desktop.
Save carlwoodhouse/0108ae1166358d3f1131035fef020fce to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;
using Patient.Framework.Routing;
namespace Patient.Forums {
public class Routes : IRouteProvider {
private const string _area = "Patient.Forums";
private readonly IForumControllerRouteConstraint _constraint;
public Routes(IForumControllerRouteConstraint constraint) {
_constraint = constraint;
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
public IEnumerable<RouteDescriptor> GetRoutes() {
return new[]
{
RouteProviderExtensions.CreateRouteDescriptor("forums", "Index", "Home", _area),
CreateRouteDescriptor("categories/{categoryNameSlugged}-{id}", "Index", "Categories", "ForumCategory"),
... more routes
CreateRouteDescriptorWithOptionalParams("me/discussions/{viewType}", "MyDiscussions", "User", new RouteValueDictionary { { "viewType", UrlParameter.Optional } }, "ForumMyDiscussions"),
CreateRouteDescriptorWithOptionalParams("{controller}/{action}/{id}", "Index", "Home", new RouteValueDictionary { { "id", UrlParameter.Optional } }, "ForumDefault"),
};
}
private RouteDescriptor CreateRouteDescriptor(string route, string action, string controller, string routeName = null) {
return RouteProviderExtensions.CreateRouteDescriptor(string.Concat("forums/", route), action, controller, _area, routeName);
}
private RouteDescriptor CreateRouteDescriptorWithOptionalParams(string route, string action, string controller, RouteValueDictionary routeParams, string routeName = null) {
if (routeParams == null) {
routeParams = new RouteValueDictionary();
}
routeParams.Add("area", _area);
routeParams.Add("controller", controller);
routeParams.Add("action", action);
var descriptor = new RouteDescriptor {
Route = new Route(string.Concat("forums/", route),
routeParams,
new RouteValueDictionary{ { "controller", _constraint } },
new RouteValueDictionary {
{ "area", _area}
},
new MvcRouteHandler())
};
if (!string.IsNullOrEmpty(routeName)) {
descriptor.Name = routeName;
}
return descriptor;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment