I am using System.Web.Mvc.Controller
for the UI and System.Web.Http.ApiController
for the API in prototyping a web interface for large ERP application. I have figured out a way to make the UI somewhat extensible with the question Deploying un-compiled ASP.NET MVC Razor application. Now I am wondering, due to the strict nature of ApiController
if there is some other class I should be considering for providing an open-ended interface for defining custom API transactions. Or is there some way to use ApiController
in a more open-ended way where parameter count and type may be varied... perhaps by accepting an object parameter?

- 1
- 1

- 25,079
- 9
- 80
- 146
2 Answers
For Web API, you could try implementing a custom action selector using IHttpActionSelector interface:
public class CustomActionSelector : IHttpActionSelector
{
public override HttpActionDescriptor SelectAction(HttpControllerContext context)
{
var method = GetMethod(context);
return new ReflectedHttpActionDescriptor(GetController(method), method);
}
private MethodInfo GetMethod(HttpControllerContext context)
{
// Locate the target method using the extensibility framework of your choice
// (for example, MEF, pure reflection, etc.)
}
private HttpControllerDescriptor GetController(MethodInfo method)
{
return new HttpControllerDescriptor()
{
ControllerName = method.DeclaringType.Name,
ControllerType = method.DeclaringType
};
}
}
To register your new action selector place the following in your global.asax
file under Application_Start
:
var config = GlobalConfiguration.Configuration;
config.Services.Replace(typeof(IHttpActionSelector), new CustomActionSelector());
Hope this helps.

- 5,090
- 19
- 27
-
Too bad that HttpControllerConfigurationAttribute is not in MVC4; it would have allowed me to specify an IHttpActionSelector only for one ApiController. Is there still any way to target 1 ApiController with the custom action selector? – BlueMonkMN Jan 09 '14 at 16:45
-
Right now, my controller class is selected by a routing defined in `WebApiConfig`. Does that mean my controller class (if it relies on that routing) has to be derived from `ApiController`. If I want to consider another kind of controller, would I have to also implement `IHttpControllerSelector`? – BlueMonkMN Jan 09 '14 at 18:31
-
@BlueMonkMN 1) I don't think it's possible 2) I'd say, you'd need something derived from `ApiController` (haven't tried not deriving, but I'd assume the framework casts the type internally to set the `ControllerContext` and other stuff). – volpav Jan 10 '14 at 07:50
To make an ASP.NET web application extensible is very straightforward because ASP.NET searches the bin directory for controller classes in all assemblies there. So if the party providing customizations can simply compile their code into a DLL and drop it into the bin directory, your web application will pick up all their controllers as well as the controllers from the standard delivery. As an example, I created the following class in a standalone DLL that referenced System.Web.Http
and System.Web.Mvc
:
Public Class CustomTestController
Inherits ApiController
Public Function GetValues() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
End Class
I simply compiled it and copied it to the bin directory of the location where my web application was deployed, and then I could access http://localhost/MyApplication/api/CustomTest/
and get back value1
and value2
in the expected response.

- 25,079
- 9
- 80
- 146