Fork me on GitHub

WinServer2008R2 + IIS 7.5 + .NET4.0 经典模式 运行WebAPI程序报404错误的解决方案

在Windows Server 2008 R2系统下,IIS 7.5 + .NET Framework 4.0的运行环境,以经典模式(Classic Mode)部署一个用.NET 4.0编译的 WebAPI 程序。

这是非常简易的一个Demo,只有一个TestController和WebApiConfig,并且已确保在Global.asax的Application_Start事件中调用WebApiConfig注册路由,大体如下:

 1 using System;
 2 using System.Web.Http;
 3 
 4 namespace WebAPI
 5 {
 6     public class Global : System.Web.HttpApplication
 7     {
 8         protected void Application_Start(object sender, EventArgs e)
 9         {
10             WebApiConfig.Register(GlobalConfiguration.Configuration);
11         }
12     }
13 }
Global
 1 using System.Web.Http;
 2 
 3 namespace WebAPI
 4 {
 5     public static class WebApiConfig
 6     {
 7         public static void Register(HttpConfiguration config)
 8         {
 9             //配置路由
10             config.Routes.MapHttpRoute(
11                 name: "DefaultApi3",
12                 routeTemplate: "api/{controller}/{action}/{id}",
13                 defaults: new { id = RouteParameter.Optional }
14             );
15 
16             config.Routes.MapHttpRoute(
17                 name: "DefaultApi2",
18                 routeTemplate: "api/{controller}/{action}"
19             );
20             
21             config.Routes.MapHttpRoute(
22                 name: "DefaultApi1",
23                 routeTemplate: "api/{controller}/{id}",
24                 defaults: new { id = RouteParameter.Optional }
25             );
26         }
27     }
28 }
WebApiConfig
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web.Http;
 4 
 5 namespace WebAPI.Controllers
 6 {
 7     public class TestController : ApiController
 8     {
 9         // GET api/test
10         public IEnumerable<string> Get()
11         {
12             return new string[] { "value1", "value2", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") };
13         }
14 
15         // GET api/test/5
16         public string Get(int id)
17         {
18             return "value";
19         }
20 
21         // POST api/test
22         public void Post([FromBody]string value)
23         {
24         }
25 
26         // PUT api/test/5
27         public void Put(int id, [FromBody]string value)
28         {
29         }
30 
31         // DELETE api/test/5
32         public void Delete(int id)
33         {
34         }
35     }
36 }
TestController
 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!--
 4   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 5   http://go.microsoft.com/fwlink/?LinkId=169433
 6   -->
 7 
 8 <configuration>
 9   <system.web>
10     <compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
11   </system.web>
12 
13   <system.webServer>
14     <handlers>
15       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
16       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
17       <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
18       <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
19       <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
20       <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
21     </handlers>
22   </system.webServer>
23 </configuration>
web.config

 本地IIS 7.5是没有问题的,直接访问localhost/api/test,成功输出XML格式的数据。

但是发布上去之后,却报404错误。

搜了一些解决方案,都是比较片面不全的,没一个能起作用。

比如,让我们在<system.webServer>节点下加<modules>或<validation>或两个都加上,像这样:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!--
 4   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 5   http://go.microsoft.com/fwlink/?LinkId=169433
 6   -->
 7 
 8 <configuration>
 9   <system.web>
10     <compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
11   </system.web>
12 
13   <system.webServer>
14     <modules runAllManagedModulesForAllRequests="true">
15       <remove name="WebDAVModule" />
16     </modules>
17     <validation validateIntegratedModeConfiguration="false" />
18     <handlers>
19       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
20       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
21       <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
22       <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
23       <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
24       <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
25     </handlers>
26   </system.webServer>
27 </configuration>

又或者,让我们在站点的处理程序映射里添加通配符脚本映射,像这样:

按照上面这些做完之后,还是不行,百思不得其解。

最后几经折腾,终于解决!!!

流程如下:

第一步,更改WebAPI站点的web.config文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!--
 4   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 5   http://go.microsoft.com/fwlink/?LinkId=169433
 6   -->
 7 
 8 <configuration>
 9   <system.web>
10     <compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
11   </system.web>
12 
13 <system.webServer>
14         <!--以下配置为了让IIS7+支持Put/Delete方法-->
15         <httpProtocol>
16             <customHeaders>
17                 <add name="Access-Control-Allow-Origin" value="*" />
18                 <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
19                 <add name="Access-Control-Allow-Headers" value="Content-Type" />
20             </customHeaders>
21         </httpProtocol>
22         <!--IIS7/7.5上必须加这个配置,否则访问报错-->
23         <modules runAllManagedModulesForAllRequests="true">
24             <remove name="WebDAVModule" />
25         </modules>
26         <validation validateIntegratedModeConfiguration="false" />
27         <handlers>
28             <remove name="WebDAV" />
29             <remove name="WebAPI_64bit" />
30             <remove name="WebAPI_32bit" />
31             <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
32             <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
33             <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
34             <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
35             <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
36             <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
37             <add name="WebAPI_64bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness64" />
38             <add name="WebAPI_32bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
39         </handlers>
40         <!--提高GET URL长度限制上限-->
41         <security>
42             <requestFiltering>
43                 <requestLimits maxUrl="409600" maxQueryString="204800" />
44             </requestFiltering>
45         </security>
46     </system.webServer>
47 </configuration>
web.config

第二步,无论是否确定已注册,都再注册一次:

Win+R键输入cmd确定,打开命令提示符(Win7以上系统都需要以管理员身份运行,且当前登录账户最好是Administrator)。

依次输入如下命令,并且按确定,等待执行完毕:

cd /d %windir%\Microsoft.NET\Framework\v4.0.30319

aspnet_regiis -i

如果服务器是64位系统,你还需要输入如下命令:

cd /d %windir%\Microsoft.NET\Framework64\v4.0.30319

aspnet_regiis -i

第三步,清理IIS站点垃圾(缓存):

 在资源管理器中,打开%windir%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files文件夹

删除该文件夹下的所有文件和文件夹,root文件夹除外,但root文件夹下的文件和文件夹也要删除。

如果碰到无法删除,提示权限不足或该文件/文件夹被占用之类的,请先停止IIS服务(在运行窗口或cmd窗口输入iisreset /stop可以停止IIS服务),再重试删除。

第四步,重启服务器。

第五步,在运行窗口或cmd窗口输入iisreset /start启动IIS服务,接着访问yourserver.com/api/xxx,成功响应并且返回数据,大功告成!!!

 

posted @ 2016-06-22 13:09  VAllen  阅读(14754)  评论(2编辑  收藏  举报