I am new to MVC and the web-api. Is it possible to have a ApiController that has a name that doesn't end in Controller? I have an MVC web application that has a FooController and a FooView. I want to create a ApiController to handle the api calls from
the FooView and other applications. How could I name it FooApi and still have it picked up by the framework? Should I do it another way?
If you want to change the way the controllers are found, you can provide your own
IHttpControllerFactory, once you implement it you can start supporting controllers whose class name doesn't end in the "Controller" suffix (you'd set your factory implementation in the configuration's service resolver).
You probably don't need that, though, for your example. If all you want is to map incoming calls to /Foo to the FooController (classic MVC) and /api/Foo to the FooApiController (web API), you can use routing for that (after renaming the FooApi class to FooApiController):
routes.MapHttpRoute("FooApi", "/api/foo", new { controller = "FooApi" });
JoeS243
0 Points
1 Post
Controller Names
Feb 25, 2012 01:57 PM|LINK
Hi-
I am new to MVC and the web-api. Is it possible to have a ApiController that has a name that doesn't end in Controller? I have an MVC web application that has a FooController and a FooView. I want to create a ApiController to handle the api calls from the FooView and other applications. How could I name it FooApi and still have it picked up by the framework? Should I do it another way?
Thanks!
Joe
silgar
Member
16 Points
15 Posts
Re: Controller Names
Feb 25, 2012 02:12 PM|LINK
Take a look at this article.
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
It says:
"To find the controller, Web API adds "Controller" to the value of the {controller} variable."
Means:
Let's say you defined a routeTemplate in Global.asax something like this:
api/{controller}
and you made a call:
api/foo
foo will be replaced as foocontroller by ApiController to find the controller class. It seems that, there is no way.
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: Controller Names
Feb 25, 2012 02:38 PM|LINK
If you want to change the way the controllers are found, you can provide your own IHttpControllerFactory, once you implement it you can start supporting controllers whose class name doesn't end in the "Controller" suffix (you'd set your factory implementation in the configuration's service resolver).
You probably don't need that, though, for your example. If all you want is to map incoming calls to /Foo to the FooController (classic MVC) and /api/Foo to the FooApiController (web API), you can use routing for that (after renaming the FooApi class to FooApiController):
routes.MapHttpRoute("FooApi", "/api/foo", new { controller = "FooApi" });
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: Controller Names
Feb 29, 2012 07:00 PM|LINK
By the way, I wrote a sample about this issue, which can be found at http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/29/supporting-different-controller-names-in-asp-net-web-apis.aspx.