This project is read-only.

Base Route

Oct 19, 2012 at 2:32 AM

I have a base route "xx/{xx_xx}" but how do I catch requests that do not match the route like my_site/, currently this returns a 404. 

Oct 19, 2012 at 2:42 AM
Edited Oct 19, 2012 at 2:46 AM

So far I using two code routes one with the base rout and one without, in the code route with the base route I have now added RootOnly which seems to be the way to do it.

 settings: new CodeRoutingSettings
     {
                UseImplicitIdToken = true,
                RootOnly = true
            });
Oct 19, 2012 at 2:52 AM

Yes, that's one way to do it, and the simplest one.

The other is to use the CustomRoute attribute on the controller, like this:

[CustomRoute("xx/{xx_xx}/{controller}")]
public abstract class MyBaseController : Controller {

   [FromRoute]
   public string xx_xx { get; set; }

   protected override void Initialize(ControllerContext context) {
      base.Initialize(context);
      this.BindRouteProperties();
   }
}

public class MyController : MyBaseController { }
I think the above should work but I'm not 100% sure.

Oct 19, 2012 at 4:42 AM

How would this work with nested controllers, when I create a nested controller the namespace still effects the url even with an updated custom route. I expected the child controller to take on the parents custom route as the base.

namespace Controllers.yy

{

[CustomRoute("xx/{xx_xx}/x/{controller}")]

}

which produces this  yy/xx/{xx_xx}/x/{controller}/{action}

when I expect this xx/{xx_xx}/x/{controller}/{action}

Oct 19, 2012 at 5:25 AM

Right, forgot about that. CustomRoute on the controller is not suited for what you need, MapCodeRoutes is the way to go.