How do I set the default namespaces in MapHttpRoute?http://forums.asp.net/t/1772699.aspx/1?How+do+I+set+the+default+namespaces+in+MapHttpRoute+Thu, 22 Mar 2012 14:44:15 -040017726994845906http://forums.asp.net/p/1772699/4845906.aspx/1?How+do+I+set+the+default+namespaces+in+MapHttpRoute+How do I set the default namespaces in MapHttpRoute? <p>With the standard MapRoute method a can pass a string collection representing the namespaces in which to search for my controller. This seems to have dissapeared from&nbsp;MapHttpRoute. How does one define the default namespaces using the new API routing?</p> 2012-02-22T20:34:21-05:004845967http://forums.asp.net/p/1772699/4845967.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>There is no default namespace. webapi searches for controllers from all the namespaces in the referenced dlls. A controller has to be </p> <ol> <li>class </li><li>public </li><li>name ends with &quot;controller&quot; (ignore case) </li><li>not abstract </li><li>implments IHttpController </li></ol> 2012-02-22T21:38:10-05:004845976http://forums.asp.net/p/1772699/4845976.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>MSDN seems to allude that its possible. In the docs for MapHttpRoute the following is indicated:</p> <p>&quot;<span>Maps the specified route template and sets default constraints, and namespaces.&quot;</span></p> <p></p> <p>http://msdn.microsoft.com/en-us/library/hh834863(v=vs.108).aspx</p> 2012-02-22T21:51:02-05:004847780http://forums.asp.net/p/1772699/4847780.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>You're right -- they simply don't provide an extension method to accept the preferred namespaces. You can always go do this manually, or do something like this (not tried it myself):</p> <pre class="prettyprint">var r = routes.MapHttpRoute( name: &quot;DefaultApi&quot;, routeTemplate: &quot;api/{controller}/{id}&quot;, defaults: new { id = RouteParameter.Optional } ); r.DataTokens[&quot;Namespaces&quot;] = new string[] {&quot;Foo&quot;};</pre> 2012-02-23T16:55:03-05:004848236http://forums.asp.net/p/1772699/4848236.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>Thanks, thats what i was looking for.</p> 2012-02-24T01:07:02-05:004850460http://forums.asp.net/p/1772699/4850460.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>Hi,</p> <p>I have the same issue. This is my case:</p> <pre class="prettyprint">namespace Client { public class DetailController : ApiController { . . . } } namespace Order { public class DetailController : ApiController { . . . } } //------------ // Global.asax //------------ . . . public static void RegisterRoutes(RouteCollection routes) { var Route1 = routes.MapHttpRoute ( name: &quot;ClientApi&quot;, routeTemplate: &quot;client/{controller}/{param1}&quot; ); Route1.DataTokens[&quot;Namespaces&quot;] = new string[] {&quot;Client&quot;}; var Route2 = routes.MapHttpRoute ( name: &quot;OrderApi&quot;, routeTemplate: &quot;order/{controller}/{param1}&quot; ); Route2.DataTokens[&quot;Namespaces&quot;] = new string[] { &quot;Order&quot; }; } . . .</pre> <p></p> <p>What I'm trying to do?</p> <p>I want to make api calls like this:</p> <p>http(s)://myapi/client/detail/PARAM1 --&gt; supposed to &quot;show&quot; detail of CLIENT named PARAM1</p> <p>http(s)://myapi/order/detail/PARAM1 --&gt; supposed to &quot;show&quot; detail of ORDER named &quot;PARAM1</p> <p></p> <p>As you see, there are two ApiControllers, &quot;<strong>DetailController</strong>&quot;, have the same name but in different Namespaces. Compiler doesn't throw an error but when I make a call, &quot;<span><em><strong>Multiple types were found that match the controller named 'detail'. This can happen if the route that services this request ('client/{controller}/{param1}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. The request for 'detail' has found the following matching controllers: ...client.DetailController ...order.DetailController</strong></em>&quot;</span></p> <p>It suggests me to use <strong>MapRoute</strong> instead of <strong>MapHttpRoute</strong>.</p> <p>How can I solve this problem?</p> 2012-02-25T09:50:48-05:004881466http://forums.asp.net/p/1772699/4881466.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <p>I solved it by implementing my own IHttpControllerFactory (and injecting via an IDependencyResolver) and can now use areas and duplicate controller names (made unique by area). I could go further and do the namespaces thing.</p> <p>It didn't seem that hard replacing this factory, looking at the DefaultHttpControllerFactory it looks like it maintaines a cache of controller name -&gt; HttpControllerDescriptor. So you can check route data to see if it contains an area and use that to qualify your key in your own cache.</p> 2012-03-15T09:42:24-04:004893961http://forums.asp.net/p/1772699/4893961.aspx/1?Re+How+do+I+set+the+default+namespaces+in+MapHttpRoute+Re: How do I set the default namespaces in MapHttpRoute? <pre class="prettyprint">r.DataTokens[&quot;Namespaces&quot;] = new string[] {&quot;Foo&quot;};</pre> <p>That really didn't work for me in my scenario.&nbsp; I'll just wait until the next release.&nbsp; I'm sure it will get better then.&nbsp;&nbsp; It looks like from the code comments MapHttpRoute should be like MapRoute but just isn't quite done yet.</p> 2012-03-22T14:44:15-04:00