WebApi 2.0 - ApiControllers in another Assembly do not undergo mapping if runAllManagedModulesForAllRequests="false"

1

I have a WebApi2 application that implements some ApiControllers .

This same application references an Assembly that implements some more.

All these controllers use Attribute Routing , as in the following example:

[RoutePrefix("sample1.endpoint")]
public class SampleController : ApiController
{
    [Route("")]
    [HttpGet]
    public HttpResponseMessage WebApiTest()

If I define runAllManagedModulesForAllRequests="true" in web.config , the application works perfectly - but I want to turn off this attribute (which can be quite costly in production).

However, if I set the value from runAllManagedModulesForAllRequests to false , only the local ApiControllers are correctly mapped; calls to others generate a 404 .

Which part, probably obvious, did I not implement?

(cross-post: link )

    
asked by anonymous 31.03.2015 / 21:06

1 answer

2

According to the answer in SOEN , the problem is that the module is not loading. To do this, just call any method of the module to be able to guarantee that it was loaded, before calling MapHttpAttributeRoutes .

The answer suggests that you create the following method in your external library:

public static class MyApiConfig {
  public static void Register(HttpConfiguration config) {
      config.MapHttpAttributeRoutes();
  }
}

And let's call this method to register the module.

You will have problems if there are multiple modules, because then the MapHttpAttributeRoutes method would be called several times.

If this is the case, you can create a method that does not receive anything, called LoadModule , whose only operation is going to be something the compiler can not solve to eliminate the method:

public static class MyApiConfig {
  public static Type LoadModule() {
      return typeof(MyApiConfig);
  }
}

You should call this method from each module before calling MapHttpAttributeRoutes .

    
31.03.2015 / 21:24