Keywords: Web API | IIS 7.5 | HTTP 404 | Routing Configuration | ExtensionlessUrlHandler
Abstract: This article provides an in-depth analysis of HTTP 404 errors when deploying Web API applications to IIS 7.5, focusing on configuring the ExtensionlessUrlHandler to resolve routing issues, while exploring alternative solutions and best practices including module configuration, ASP.NET registration, and route adjustments.
Problem Background and Diagnosis
When migrating Web API applications from Visual Studio development server to IIS 7.5 production environment, developers frequently encounter HTTP 404 errors. This issue typically stems from mismatches between IIS configuration and Web API routing mechanisms. While the development server properly handles extensionless URL requests, IIS requires explicit handler configuration to achieve the same functionality.
Core Solution: ExtensionlessUrlHandler Configuration
The most effective solution involves modifying the <system.webServer> section in the web.config file to add configuration for the ExtensionlessUrlHandler-Integrated-4.0 handler. The specific implementation is as follows:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>The key aspects of this configuration include:
path="*."matches all extensionless URL pathsverb="*"supports all HTTP methods (GET, POST, PUT, DELETE, etc.)preCondition="integratedMode,runtimeVersionv4.0"ensures operation in integrated mode with .NET 4.0
Alternative Solution Analysis
Another common configuration uses runAllManagedModulesForAllRequests="true", but this is not considered best practice. This setting forces all managed modules to process every request, potentially causing performance issues and security vulnerabilities. A more elegant solution involves explicit configuration of UrlRoutingModule:
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>Environment Configuration Verification
Ensuring proper ASP.NET registration with IIS is crucial. If IIS was installed after ASP.NET, manual registration is required:
- Run Command Prompt as administrator
- Navigate to the appropriate .NET Framework directory (e.g.,
C:\Windows\Microsoft.NET\Framework64\v4.0.30319) - Execute the
aspnet_regiis.exe -icommand
Routing Configuration Considerations
When Web API is deployed in virtual directories or sub-applications, routing configurations require appropriate adjustments. For example, when migrating from localhost:3092/api/values to localhost/SampleWebAPI/api/values, it may be necessary to modify route templates in Global.asax to ensure proper URL mapping.
Best Practices Summary
The optimal approach to resolving Web API 404 errors in IIS 7.5 involves combining the aforementioned techniques: prioritize ExtensionlessUrlHandler configuration, verify ASP.NET registration when necessary, and adjust routing configurations based on deployment environment. Avoid global settings like runAllManagedModulesForAllRequests="true" in favor of targeted module configurations.