This project is read-only.

Example for setting up routing with an APIController

Oct 8, 2013 at 10:28 PM
I've started using your framework and it seems to work really well for my needs. I'm at a point where I am wiring up some APIControllers along side my already functional standard Controller framework. I noticed from one of the Oct 2012 discussions in this forum that you have provided ApiController support. However I don't see how to wire this up.

I am using v1.1 of your framework (pulled from Nuget). Can you please provide sample code for wiring one up?

Thanks for an excellent contribution!
Oct 8, 2013 at 10:35 PM
You need to install the MvcCodeRouting.Web.Http pre-release package.

Max Toro
Oct 9, 2013 at 3:17 PM
Ok, I found the site and downloaded/installed the package without issue.

Now what? Does the route.MapCodeRoutes need any special config? Do I need to following any special naming conventions (eg: the api/endpoint style naming that MS provides).

A small example would be very helpful / greatly appreciated!
Oct 9, 2013 at 3:44 PM
No special convention/configuration is needed. If the ApiController is under the root namespace you are currently using then it should show up in routes.axd. If not then just call MapCodeRoutes again using a baseRoute.
Oct 14, 2013 at 8:17 PM
Edited Oct 14, 2013 at 8:32 PM
Something is amiss here for me. Your help pinpointing is appreciated. First here is the output of my routes.axd.
// MvcCodeRouting v1.1.0
// Format: C# - Visual Basic

routes.MapHttpRoute(null, "api/{controller}/{id}", 
    new { id = RouteParameter.Optional });

routes.MapRoute(null, "{action}", 
    new { controller = @"Home", action = @"Index" }, 
    new { action = @"Index" }, 
    new[] { "Bpm.Controllers" });

routes.MapRoute(null, "Bff/Ui/{action}", 
    new { controller = @"Ui" }, 
    new { action = @"Status" }, 
    new[] { "Bpm.Controllers.Bff" });

routes.MapRoute(null, "Rp/Ui/{action}", 
    new { controller = @"Ui" }, 
    new { action = @"Summary" }, 
    new[] { "Bpm.Controllers.Rp" });
You can see I've created a namespace tree for Bpm.Controllers.[area]
  • For all my controllers that inherit from System.Web.Mvc.Controller - the routes were created properly.
    1. Bpm.Controllers.Bff.UiController
    2. Bpm.Controllers.Rp.UiController
I can access both of these using the restful url endpoints /bpm/ui/status or rp/ui/summary
  • However I also have 2 controllers which inherit from System.Web.Http.ApiController.
    1. Bpm.Controllers.Bff.ImportController
    2. Bpm.Controllers.Rp.ImportController
I want to be able to access them using the same restful endpoints as the non-ApiControllers above (eg: /bpm/import/1 or rp/import/1).

As it stands I have to fall back to the MS api/{controller}/{id} syntax that your framework was designed to avoid.

What am I missing here?
Oct 14, 2013 at 10:14 PM
I cannot see the issue from the information you provided. You can:
  • Download the source code and see/run the samples project, it contains some ApiController(s), maybe by looking at it you can find out what's wrong with your project.
  • Send me a trimmed-down version of your project that contains only the necesary code to reproduce the problem, to and I will take a look.
Oct 15, 2013 at 4:31 PM
I took your advice and downloaded the source code. Once I "proved out" that the apiControllers could be organized alongside regular controllers in a restful manner I dug into my code a little more and found the problem. Apparently the Nuget package didn't automatically reference the MvcCodeRouting.Web.Http.dll in my projects. Once I added that reference and imported that namespace into the controllers the proper routing appeared in Routes.axd. Most excellent!

Thanks for your assistance and I am looking forward to the namespace/restful url organization/synchronization that this provides. Definitely this functionality is missing in the MS framework and I appreciate the work you've done here to add it.

Marked as answer by maxtoroq on 10/15/2013 at 9:18 AM
Oct 15, 2013 at 4:42 PM
I'm glad you found the problem. I'm also curious why it didn't work in the first place. Did you install the MvcCodeRouting.Web.Http package in the wrong project?
Oct 15, 2013 at 6:06 PM
Did you install the MvcCodeRouting.Web.Http package in the wrong project?
Not that I remember. I have 3 projects in this particular solution ( app, shared dll, and test harness). As far as I recall, after installing the nuget package it asked me which project(s) I wanted to apply it to and I selected all 3.

The only thing that may be odd about my config is that I had initially just installed using the "Install-Package MvcCodeRouting". After your advice above I ran a 2nd install using "Install-Package MvcCodeRouting.Web.Http -Pre"

Hope this helps.