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注册路由,大体如下:

 using System;
using System.Web.Http; namespace WebAPI
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
}

Global

 using System.Web.Http;

 namespace WebAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//配置路由
config.Routes.MapHttpRoute(
name: "DefaultApi3",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}"
); config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}

WebApiConfig

 using System;
using System.Collections.Generic;
using System.Web.Http; namespace WebAPI.Controllers
{
public class TestController : ApiController
{
// GET api/test
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") };
} // GET api/test/5
public string Get(int id)
{
return "value";
} // POST api/test
public void Post([FromBody]string value)
{
} // PUT api/test/5
public void Put(int id, [FromBody]string value)
{
} // DELETE api/test/5
public void Delete(int id)
{
}
}
}

TestController

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
</system.web> <system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<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" />
<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" />
<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" />
</handlers>
</system.webServer>
</configuration>

web.config

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

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

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

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

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
</system.web> <system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<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" />
<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" />
<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" />
</handlers>
</system.webServer>
</configuration>

又或者,让我们在站点的处理程序映射里添加通配符脚本映射,像这样:
WinServer2008R2 + IIS 7.5 + .NET4.0 经典模式 运行WebAPI程序报404错误的解决方案

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

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

流程如下:

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

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
</system.web> <system.webServer>
<!--以下配置为了让IIS7+支持Put/Delete方法-->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
<!--IIS7/7.5上必须加这个配置,否则访问报错-->
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="WebDAV" />
<remove name="WebAPI_64bit" />
<remove name="WebAPI_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<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" />
<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" />
<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" />
<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" />
<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" />
</handlers>
<!--提高GET URL长度限制上限-->
<security>
<requestFiltering>
<requestLimits maxUrl="409600" maxQueryString="204800" />
</requestFiltering>
</security>
</system.webServer>
</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,成功响应并且返回数据,大功告成!!!

上一篇:rpm方式安装MySQL后在命令行登录报错:ERROR 1045 (28000): Unknown error 1045


下一篇:android给View设置边框 填充颜色 弧度