Web Api 2 - Routings are not working

4

I created a web service REST using Web Api 2, and in development everything is functional. I'm using a small variation of the default route:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{numProtocolo}",
    defaults: new { numProtocolo = RouteParameter.Optional }
);

However, when the application is created in IIS (version 7, Windows 2008 Server 32-bit) and run in production, it returns 404 for routes that are valid:

    
asked by anonymous 26.02.2014 / 20:45

2 answers

1

The problem happens because IIS is not able to understand which URL rewrite mechanism to use. To resolve this, we must add the following line in our string of web.config , in <system.webServer> :

<modules runAllManagedModulesForAllRequests="true" />

The complete code will look like this (deleting any other nodes that might exist in <system.webserver> :

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

Now the routes work as they should because IIS was instructed to use the project-managed modules for all requests, rather than trying to determine which module to apply.

    
26.02.2014 / 20:45
0

In your case IIS is not able to resolve the routes. If you are using inheritance chain to implement your endpoints , use the following solution:

    public class CustomDirectRouteProvider : DefaultDirectRouteProvider
    {
        protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(
            HttpActionDescriptor actionDescriptor)
        {
            return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(true);
        }
    }

And, in your HttpConfiguration startup section:

config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
    
09.02.2016 / 19:19