views:

64

answers:

1

I have one Area and in AreaRegistration I defined namespace all controllers in area belongs to.

context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller="Home", action = "Index", id = UrlParameter.Optional },
                new[] { "MySite.Admin.Controllers" }             // Namespaces
            );

How to prevent controller in that area to be called even when not that route is matched. I.E. /home/index (without "admin" word at the beginning).

EXAMPLE:

If a have controller "MySite.Admin.Controllers.HomeController" which belongs to area root defined above. I want to forbid controller factory to search for that controller (i.e. throw exception) if it doesn't match route "Admin/{controller}/{action}/{id}" ("Admin" at the end). So if I type "home/index" (no "Admin" at the beginning) web site will throw an error that it can't find controller.

Hope i was clear enough.

+1  A: 

Add a constraint to match the area!

Here is a nice read on route constraints.

If you want to write your own custom route constraint, then give the following a read

http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/01/11/asp-net-mvc-route-constraints.aspx

Edit:

Remove the default route and make sure everything is scoped to areas, then home/index will throw an exception as it is not scoped to an area.

Mahesh Velaga
This will enable me to detect the best matched route but will not FORBID controller factory to find controller under specific area
Andrej Kaurin
I believe this removal of default routes will solve the problem but just wondering if this is a good practice?
Andrej Kaurin
This is definitely not a bad practice as you are not hacking any thing out of the framework, you are just configuring your routes for your needs, that's it! :)
Mahesh Velaga