Blankman
Blankman

Reputation: 267040

Can a route prefix a controller's name? issues with urls and controller name collisions

I want my urls structure to be like this:

www.stackoverflow.com/order/...

www.stackoverflow.com/admin/order/...

Now both of the above are using different controllers.

/controllers/ordercontroller
/controllers/admin/ordercontroller

Is there a way that I can have this url structure?

I was thinking if I could do this:

/controllers/ordercontroller
/controllers/admin/adminordercontroller

And somehow, in my routes, prefix the name of all the admin related controllers with 'admin' ?

Upvotes: 1

Views: 493

Answers (1)

womp
womp

Reputation: 116977

Sure, no problem. Remember you can have static text in routes as well.

routes.MapRoute("admin", "admin/{controller}/{action}/{id}", ....

routes.MapRoute("normal", "{controller}/{action}/{id}", ...

The key thing here is to make sure that the admin route is first, so that it has first crack at matching the URL. Otherwise the "normal" route will swallow it, because it also matches.

Upvotes: 2

Related Questions