15

I'm building a test center for various unit tests on my personal library and I wanted to have an easy-to-access route for a test API. So I have two entries in my route config: the default and my custom route...

routes.MapRoute(
            name:="Default",
            url:="{controller} / {Action} / {id}",
            defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
        )

        routes.MapHttpRoute(
                name:="UnitTests",
                routeTemplate:="test/{category}/{testname}",
                defaults:=New With {.controller = "TestApis", .action = "testme",
                .category = "TestCategoryOne", .testname = UrlParameter.Optional}
            )

The idea was that whenever I want to make a call to my test API controller, I'll just prepend the URL with "test/" and it would know to redirect it to my "TestApisController" . . . but it seems to have broken my Default route.

QUESTION

Is there a way by which I can map all routes that start with "test/" to a specific controller while at the same time not breaking my default route?

EDIT

So after some time, I realized that the reason this method wasn't working had nothing to do with the route. I had another default API route defined in the "WebApiConfig" file that was causing some issues. I apologize, I am still transitioning from Webforms to MVC.

Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
  • I am slightly confused, `MapHttpRoute` does not exist in MVC 6? The closest I know is adding the `Microsoft.AspNet.Mvc.WebApiCompatShim` which allows you to use `routes.MapWebApiRoute`. In any case, why don't you add your route as a standard route (since you are defining a controller and an action) with `routes.MapRoute`, and declare it before the default route (as the test route is more specific than the default one)? – Daniel J.G. Oct 07 '15 at 11:28
  • @DanielJ.G. I am using MapHttpRoute because the controller I am trying to call is a Web API controller. It doesn't seem to work otherwise. – Ross Brasseaux Oct 08 '15 at 00:52
  • @DanielJ.G. And that was my mistake regarding the MVC 6 tag. I am using MVC version 5.2 – Ross Brasseaux Oct 08 '15 at 01:04
  • 1
    You will need to swap the order of the routes `../test/someCategory/someTestName` will go to the `someCategory` method of `testController` passing `someTestName` to the `id` parameter because it matches the first (default) route –  Oct 08 '15 at 07:07

1 Answers1

0

Since the controller you're trying to route to is a Web Api controller you'll want to put your custom route in the WebApiConfig.vb file. The route should then go above the default route in that file because it is more specific than the default route, as @Stephen Muecke noted. Your original code would change a bit, instead of routes.MapHttpRoute you would have config.Routes.MapHttpRoute, like this:

config.Routes.MapHttpRoute( _
    name:="UnitTests", _
    routeTemplate:="test/{category}/{testname}", _
    defaults:=New With {.controller = "TestApis", .action = "testme", .category = "TestCategoryOne", .testname = UrlParameter.Optional}
)

I've done this same thing when I wanted to route to specific controllers in some situations and also included custom authentication handlers on the route, what you're trying to do should work.

AK3800
  • 2,138
  • 3
  • 23
  • 28