I'm trying to implement my own route class, inheriting from the default Route.
This is what my custom route class looks like:
public class FriendlyRoute : Route
{
public FriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(url, defaults, routeHandler)
{
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
var controllerName = routeData.Values["controller"].ToString();
var actionName = routeData.Values["action"].ToString();
routeData.Values["controller"] = fix(controllerName);
routeData.Values["action"] = fix(actionName);
return routeData;
}
private string fix(string name)
{
//Remove dashes: "my-controller" => "mycontroller"
}
}
What I'm doing is accepting urls with dashes and routing the to the correct action ("my-controller/my-action" to "MyController/MyAction"), but I have some more plans for this custom route to.
To put my custom route class in action, I use the following route config:
routes.Add("Default",
new FriendlyRoute("{controller}/{action}/{id}",
new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
new MvcRouteHandler()));
This works fine! But I'm not happy with the url structure. I want to have some urls with no controller names only action names (e.g. "/about", "/contact") and some with controller names (e.g. "/mypage", "/mypage/invoices"). I start by using the default route class (not my own custom) and fix this problem:
routes.Add("MyPages",
new Route("MyPage/{action}",
new RouteValueDictionary(new { controller = "MyPage", action = "Summary"}),
new MvcRouteHandler()));
routes.Add("Public",
new Route("{action}/{id}",
new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
new MvcRouteHandler()));
This also works fine, but now there's no support for urls with dashes. So I just swap in my custom route class into the route config:
routes.Add("MyPages",
new FriendlyRoute("MyPage/{action}",
new RouteValueDictionary(new { controller = "MyPage", action = "Summary" }),
new MvcRouteHandler()));
routes.Add("Public",
new FriendlyRoute("{action}/{id}",
new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
new MvcRouteHandler()));
Now when I run the application I try to go to the default page ("/") it crashes because the call to base.GetRouteData(httpContext) in my FriendlyRoute.GetRouteData() returns null.
I'm all new to creating a custom route class, so any hints on what I' doing wrong would be appreciated.