在IIS 7中部署时,ASP.NET Web API应用程序提供404

Cot*_*ten 53 asp.net iis-7 asp.net-web-api

我有一个ASP.NET Web API,在使用localhost:1783在"IIS Express"上运行时可以正常工作

VS中的设置

但当我没有"使用IIS Express"然后按"创建虚拟目录"时......

新设置

...我刚收到404错误: 404错误

任何想法都错了吗?谢谢!

Dav*_*Dev 39

虽然明确的答案使它工作,但您真正需要添加到webconfig的是:

    <handlers>
      <!-- Your other remove tags-->
      <remove name="UrlRoutingModule-4.0"/>
      <!-- Your other add tags-->
      <add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition=""/>
    </handlers>
Run Code Online (Sandbox Code Playgroud)

请注意,这些都没有特定的顺序,但您希望在添加之前删除它们.

我们最终获得404的原因是因为Url路由模块仅在IIS中启动网站的根目录.通过将模块添加到此应用程序的配置中,我们让模块在此应用程序的路径(您的子目录路径)下运行,并且路由模块启动.

  • 这个解决方案更好,因为对runAllManagedModulesForAllRequests ="true"有负面影响http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html (4认同)
  • 使用上面建议的设置,我收到以下错误:`system.web.urlroutingmodule没有实现iHttpHandlerFactory ...`.相反,[如此处建议](http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html),我在<configuration> <system.webserver> <modules下添加了配置>,并删除了路径和动词属性,这也引发了错误. (3认同)

the*_*ski 14

对我来说,除了runAllManagedModulesForAllRequests="true"我还必须编辑"path" 下面的属性.以前我的路径属性"*."意味着它只在包含点字符的url上执行.但是,我的应用程序的url不包含点.当我改变路径"*"然后它工作.这就是我现在拥有的:

  <system.webServer>
      <validation validateIntegratedModeConfiguration="false" />
      <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule"/>
      </modules>

      <handlers>
          <remove name="WebDAV" />
          <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="*" 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="*" 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="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)


Ton*_*ony 10

您可能需要安装Hotfix KB980368.

本文介绍了一种更新,该更新使某些Internet信息服务(IIS)7.0或IIS 7.5处理程序能够处理其URL不以句点结尾的请求.具体来说,这些处理程序映射到" ." 请求路径.目前,一个映射到"." 的处理程序.请求路径仅处理其URL以句点结尾的请求.例如,处理程序仅处理其URL类似于以下URL的请求:

http://www.example.com/ExampleSite/ExampleFile.

应用此更新后,映射到"*"的处理程序.请求路径可以处理其URL以句点结尾的请求以及其URL不以句点结尾的请求.例如,处理程序现在可以处理类似于以下URL的请求:

http://www.example.com/ExampleSite/ExampleFile

http://www.example.com/ExampleSite/ExampleFile.

应用此修补程序后,ASP.NET 4应用程序可以处理无扩展URL请求.因此,在处理程序执行之前运行的托管HttpModule将运行.在某些情况下,HttpModules可以返回无扩展URL的错误.例如,编写为仅期望.aspx请求的HttpModule现在可能在尝试访问HttpContext.Session属性时返回错误.


小智 8

由于以下原因,也可能发生此问题

1.在Web.Config中

<system.webServer>
     <modules runAllManagedModulesForAllRequests="true" /> 
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

2.确保在部署Web API的服务器上的bin文件夹中提供以下内容

•System.Net.Http

•System.Net.Http.Formatting

•System.Web.Http.WebHost

•System.Web.Http

如果通过Visual Studio发布,则默认情况下不会将这些程序集复制到bin文件夹中,因为Web API程序包是通过开发计算机中的Nuget安装的.仍然如果您想要将这些文件作为Visual Studio发布的一部分提供,那么您需要为这些程序集将CopyLocal设置为True