Web Api 2 - Routing not working

Asked

Viewed 625 times

4

I created a web service REST using Web Api 2, and in development everything is functional. I am 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 production on IIS (version 7, Windows 2008 32-bit Server) and run in production, it returns 404 for routes that are valid:

inserir a descrição da imagem aqui

2 answers

1


The problem is because IIS is failing to understand which URL rewriting engine to use. To solve it, we must add the following line in our web.config, in <system.webServer>:

<modules runAllManagedModulesForAllRequests="true" />

The complete code will look like this (deleting other nodes possibly existing in <system.webserver>:

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

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

  • It is worth taking into account that when the parameter runAllManagedModulesForAllRequests is connected, ALL requests are passed through the pipeline of ALL modules - static files including (.js, .html, etc.) In certain situations this may result in a substantial loss of performance on the site as a whole.

0

In your case IIS is unable to resolve the routes. If you are using inheritance chain to implement their 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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.