Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller in separate assembly and routing

In the same solution, there is a ASP.NET MVC4 application Slick.App and class library Awesome.Mvc.Lib. Awesome.Mvc.Lib contains one controller class.

public class ShinnyController : Controller {     [HttpGet]     public string Index()     {         return "Hello, from Awesome.Mvc.Lib";     } } 

If I just add the reference from Slick.App to Awesome.Mvc.Lib, run the application and point brower to /shinny, I will actually see the response "Hello, from Awesome.Mvc.Lib".

This is something I don't expect at all. All the time I thought ASP.NET MVC respects the namespaces there the controllers placed in. So, the controllers from another namespaces are not exposed, at least before I didn't ask to.

I tried to change the default route registration, to use namespaces parameter.

    routes.MapRoute(         name: "Default",         url: "{controller}/{action}/{id}",         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },         namespaces: new [] { "Slick.App.Controllers" }     ); 

Still, the ShinnyController route still match for '/shinny'.

I have a concerns this is right default behaviour. My question is, how to explicitly say which controllers are exposed and prevent default route to match controllers in separate class library?

like image 573
Alexander Beletsky Avatar asked Jun 18 '12 05:06

Alexander Beletsky


People also ask

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What are routers and controllers?

Router - is what defines how to parse request data. Controller - is what accepts parsed request and generates a response.

How many types of routing are there in MVC?

MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application. The earlier style of routing, called convention-based routing, is still fully supported.


1 Answers

The namespaces list on the route only gives priority to certain namespaces over the others, which are not listed :

new [] {"Namespace1", "Namespace2"} 

doesn't give higher priority to Namespace1 as one would expect but just gives priority to both namespaces over the others.

This means that the namespaces in the list are first searched for controllers and then, if no match is found the rest of the available controllers with that name are used.

You can suppress the use of non prioritized controllers by doing this:

var myRoute  = routes.MapRoute(         name: "Default",         url: "{controller}/{action}/{id}",         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },         namespaces: new [] { "Slick.App.Controllers" }     );  myRoute.DataTokens["UseNamespaceFallback"] = false; 
like image 184
Ventsyslav Raikov Avatar answered Oct 05 '22 21:10

Ventsyslav Raikov