views:

119

answers:

2

I use custom routes for my URLs and my action become accessible via two URLs (not counting trailing slash and lower\upper case letters): one via my custom route /my-custom-route-url/ and one via default /controller/action.

I see one possible solution -- put all controllers which use default routing (they are mostly backend) in one area, and place all others in separate area and use it without default route.

May be there is a better way?

+1  A: 

IMHO, this is duplication and should be avoided. You could use URL Rewriting of IIS to do this with its rewrite module.

The custom URL can be rewritten at the IIS level to use the normal controllers and actions and I think that should serve your purpose.

For more info look here: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

Mahesh Velaga
Custom Routes are good for generating outgoing URLs too, with Url Rewrite solution I should duplicate routes in URL rewrite configuration...
artvolk
I thought you were asking for all of your controllers, it was not very clear from your question if you wanted it for a particular controller or action, I suggested this if you were going to need custom routes for any of your controllers or actions.
Mahesh Velaga
When using URL Rewrite the logic of rewrite is decoupled to your application so you can change without changing the code if you decide to access contents through different custom URL.
Mahesh Velaga
I've checked URL Rewrite -- it seems it is configured in Web.config, which is good, thanks for pointing this!
artvolk
A: 

The Default Route is an unnecessary evil in my opinion. It's helpful in that it means you write less code, but really I find it's far better to simply create each or at least most routes by hand.

Areas are a good way to separate different parts of your application, but using it just to avoid dealing with a blank default route isn't the correct reason. I use areas to isolate my admin area from my default area, but my reasoning for doing so is that they are two independent systems.

If you like you can make default routes for each controller as needed on a case by case basis. For example you could have a default products/{action} route and a default home/{action} route, but then individually define each route in the ContactController or what have you.

routes.MapRoute(
    "Products_Default",
    "products/{action}",
    new {controller = "Products", action = "Index"},
    new[] {"Web.Components.Controllers"}
);

routes.MapRoute(
    "Contact_Send",
    "contact/send",
    new {controller = "Contact", action = "SendMessage"},
    new[] {"Web.Components.Controllers"}
);

routes.MapRoute(
    "Contact_Home",
    "contact",
    new {controller = "Contact", action = "Index"},
    new[] {"Web.Components.Controllers"}
);
Nathan Taylor
Thanks, defining per-controller routes are good idea!
artvolk