简体   繁体   English

ASP.NET Web API 应用程序在 IIS 7 上部署时给出 404

[英]ASP.NET Web API application gives 404 when deployed at IIS 7

I have an ASP.NET Web API which works fine when running on "IIS Express" with localhost:1783我有一个 ASP.NET Web API,它在使用 localhost:1783 的“IIS Express”上运行时工作正常

VS 中的设置

But when I uncross the "Use IIS Express" and then press "Create Virtual Directory"...但是当我取消“使用 IIS Express”然后按“创建虚拟目录”时......

新设置

...I just get 404 errors: ...我只是收到 404 错误:404错误

Any ideas whats wrong?任何想法有什么问题? Thanks!谢谢!

While the marked answer gets it working, all you really need to add to the webconfig is:虽然标记的答案让它工作,但你真正需要添加到 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>

Note that none of those have a particular order, though you want your removes before your adds.请注意,这些都没有特定的顺序,尽管您希望在添加之前删除。

The reason that we end up getting a 404 is because the Url Routing Module only kicks in for the root of the website in IIS.我们最终得到 404 的原因是因为 Url 路由模块只针对 IIS 中的网站根目录起作用。 By adding the module to this application's config, we're having the module to run under this application's path (your subdirectory path), and the routing module kicks in.通过将模块添加到此应用程序的配置中,我们让模块在此应用程序的路径(您的子目录路径)下运行,并且路由模块开始运行。

For me, in addition to having runAllManagedModulesForAllRequests="true" I also had to edit the "path" attribute below.对我来说,除了runAllManagedModulesForAllRequests="true"我还必须编辑下面的"path"属性。 Previously my path attribute was "*."以前我的路径属性是"*." which means it only executed on url's containing a dot character.这意味着它只在包含点字符的 url 上执行。 However, my application's url's don't contain a dot.但是,我的应用程序的 url 不包含点。 When I switched path to "*" then it worked.当我将路径切换到"*"它就起作用了。 Here's what I have now:这是我现在所拥有的:

  <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>

You may need to install Hotfix KB980368 .您可能需要安装修补程序KB980368

This article describes a update that enables certain Internet Information Services (IIS) 7.0 or IIS 7.5 handlers to handle requests whose URLs do not end with a period.本文介绍了一种更新,该更新使某些 Internet 信息服务 (IIS) 7.0 或 IIS 7.5 处理程序能够处理其 URL 不以句点结尾的请求。 Specifically, these handlers are mapped to " ."具体来说,这些处理程序被映射到“ .”。 request paths.请求路径。 Currently, a handler that is mapped to a " ."当前,映射到“ .”的处理程序 request path handles only requests whose URLs end with a period.请求路径仅处理 URL 以句点结尾的请求。 For example, the handler handles only requests whose URLs resemble the following URL:例如,处理程序仅处理其 URL 类似于以下 URL 的请求:

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

After you apply this update, handlers that are mapped to a "*."应用此更新后,映射到“*”的处理程序。 request path can handle requests whose URLs end with a period and requests whose URLs do not end with a period. request path 可以处理 URL 以句点结尾的请求和 URL 不以句点结尾的请求。 For example, the handler can now handle requests that resemble the following URLs:例如,处理程序现在可以处理类似于以下 URL 的请求:

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

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

After this patch is applied, ASP.NET 4 applications can handle requests for extensionless URLs.应用此补丁后,ASP.NET 4 应用程序可以处理对无扩展名 URL 的请求。 Therefore, managed HttpModules that run prior to handler execution will run.因此,在处理程序执行之前运行的托管 HttpModules 将运行。 In some cases, the HttpModules can return errors for extensionless URLs.在某些情况下,HttpModules 可以为无扩展名的 URL 返回错误。 For example, an HttpModule that was written to expect only .aspx requests may now return errors when it tries to access the HttpContext.Session property.例如,编写为仅期望 .aspx 请求的 HttpModule 现在在尝试访问 HttpContext.Session 属性时可能会返回错误。

This issue can also happen due to the following由于以下原因也可能发生此问题

1.In the Web.Config 1.在Web.Config中

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

2.Make sure the following are available in the bin folder on the server where the Web API is deployed 2.确保部署Web API的服务器上的bin文件夹中有以下内容

•System.Net.Http •System.Net.Http

•System.Net.Http.Formatting •System.Net.Http.Formatting

•System.Web.Http.WebHost •System.Web.Http.WebHost

•System.Web.Http •系统.Web.Http

These assemblies won't be copied in the bin folder by default if the publish is through Visual Studio because the Web API packages are installed through Nuget in the development machine.如果通过 Visual Studio 发布,则默认情况下不会将这些程序集复制到 bin 文件夹中,因为 Web API 包是通过开发计算机中的 Nuget 安装的。 Still if you want to achieve these files to be available as part of Visual Studio publish then you need to set CopyLocal to True for these Assemblies尽管如此,如果您想让这些文件作为 Visual Studio 发布的一部分可用,那么您需要将这些程序集的 CopyLocal 设置为 True

Some people say runAllManagedModulesForAllRequests="true" will have performance issues and MVC routing issues.有人说 runAllManagedModulesForAllRequests="true" 会有性能问题和 MVC 路由问题。 They suggest to use the following:他们建议使用以下内容:

http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html

http://bartwullems.blogspot.com/2012/06/optimize-performance-of-your-web.html http://bartwullems.blogspot.com/2012/06/optimize-performance-of-your-web.html

For me, this issue was slightly different than other answers, as I was only receiving 404s on OPTIONS, yet I already had OPTIONS specifically stated in my Integrated Extensionless URL Handler options.对我来说,这个问题与其他答案略有不同,因为我只在 OPTIONS 上收到 404,但我已经在我的 Integrated Extensionless URL Handler 选项中明确说明了 OPTIONS。 Very confusing.很混乱。

  1. As others have stated, runAllManagedModulesForAllRequests="true" in the modules node is an easy way to blanket-fix most Web API 404 issues - although I prefer @DavidAndroidDev 's answer which is much less intrusive.正如其他人所说,模块节点中的 runAllManagedModulesForAllRequests="true" 是一种全面修复大多数 Web API 404 问题的简单方法 - 尽管我更喜欢 @DavidAndroidDev 的答案,它的干扰性要小得多。 But there was something additional in my case.但在我的情况下还有一些额外的东西。
  2. Unfortunately, I had this set in IIS under Request Filtering in the site:不幸的是,我在站点的请求过滤下的 IIS 中设置了这个:

OPTIONS 请求过滤问题

By adding the following security node to the web.config was necessary to knock that out - full system.webserver included for context:通过将以下安全节点添加到 web.config 是必要的 - 完整的 system.webserver 包含用于上下文:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <security>
      <requestFiltering>
        <verbs>
          <remove verb="OPTIONS" />
        </verbs>
      </requestFiltering>
    </security>
  </system.webServer>

Although it's not the perfect answer for this question, it is the first result for "IIS OPTIONS 404" on Google, so I hope this helps someone out;虽然这不是这个问题的完美答案,但它是谷歌上“IIS OPTIONS 404”的第一个结果,所以我希望这能帮助别人; cost me an hour today.今天花了我一个小时。

I you are using Visual Studio 2012, download and install Update 2 that Microsoft released recently (as of 4/2013).如果您使用的是 Visual Studio 2012,请下载并安装 Microsoft 最近发布的更新 2(截至 2013 年 4 月)。

Visual Studio 2012 Update 2 Visual Studio 2012 更新 2

There are some bug fixes in that update related to the issue.该更新中有一些与问题相关的错误修复。

I had as same problem .我有同样的问题。 afer lot of R&D i found the problem.经过大量的研发,我发现了这个问题。

but as long as your configuration are finne mean that aspnet 64 bit and the IIS well then the only problem i saw is the path " web api taking the local directiry path" so that you need to avid it.但只要你的配置是 finne 意味着 aspnet 64 位和 IIS 好那么我看到的唯一问题是路径“web api 采用本地目录路径”,所以你需要喜欢它。 by like this.. ~../../../api/products/像这样.. ~../../../api/products/

thank you very much for posting the problem.非常感谢您发布问题。 i leanred alot abt iis and other setting in config file.我在配置文件中学习了很多 abt iis 和其他设置。

I have been battling this problem for a couple of days trying all kinds of things suggested.几天来,我一直在与这个问题作斗争,尝试各种建议。 My dev machine was working fine, but the new machine I was deploying to was giving me the 404 error.我的开发机器工作正常,但我部署到的新机器给了我 404 错误。

In IIS manager, I compared the handler mappings on both machines to realize that a lot of handlers were missing.在 IIS 管理器中,我比较了两台机器上的处理程序映射,发现缺少很多处理程序。 Turns out that ASP.Net 5 was not installed on the machine.结果发现机器上没有安装 ASP.Net 5。

For me I received a 404 error on my websites NOT using IIS Express (using Local IIS) while running an application that WAS using IIS Express.对我来说,在运行使用 IIS Express 的应用程序时,我在我的网站上收到了 404 错误,而不是使用 IIS Express(使用本地 IIS)。 If I would close the browser that was used to run IIS Express, then the 404 would go away.如果我关闭用于运行 IIS Express 的浏览器,那么 404 就会消失。 For me I had my IIS Express project calling into Local IIS services, so I converted the IIS Express project to use Local IIS and then everything worked.对我来说,我的 IIS Express 项目调用了本地 IIS 服务,因此我将 IIS Express 项目转换为使用本地 IIS,然后一切正常。 It seems that you can't run both a non-IIS Express and Local IIS website at the same time for some reason.由于某种原因,您似乎无法同时运行非 IIS Express 和本地 IIS 网站。

Spend a whole week, after all the following setting worked !花了整整一周的时间,毕竟以下设置都奏效了! and finally saved.并最终得救。 Removing the UrlScan from ISAPI fileters in IIS fixes the problem in our case从 IIS 中的 ISAPI 过滤器中删除 UrlScan 解决了我们案例中的问题

I had this problem with Blazor and found that incorporating NavManagers "baseUrl" into my calls to the controller solved the issue regardless of using a Virtual Directory or the root website.我在 Blazor 上遇到了这个问题,发现将 NavManagers“baseUrl”合并到我对控制器的调用中解决了这个问题,无论使用虚拟目录还是根网站。

This worked for me!这对我有用!

string baseUrl = NavigationManager.BaseUri.ToString();

NavigationManager.NavigateTo(**baseUrl** + $"api/Download/DownloadFile?FileName=" + sFilename, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 2.2 Web API在IIS上托管后提供404 - ASP.NET Core 2.2 Web API gives 404 after hosting on IIS 如果作为子应用程序部署在IIS中,则ASP.NET Web API中的路由问题 - Routing problems in ASP.NET Web API if deployed in IIS as child application IIS7上的ASP.NET Web API-找不到404 - ASP.NET Web API on IIS7 - Not Found 404 自托管 ASP.NET Web API 时未找到 404,在 IIS 中工作正常 - 404 not found when self hosting ASP.NET Web API, works fine within IIS 路由上的IIS 8.5 ASP.NET Web Api 2 404,除非文件系统中存在它们 - IIS 8.5 ASP.NET Web Api 2 404's on routes except when they exist on filesystem 在本地IIS上以及在部署到Azure时,ASP.NET MVC处理404错误的方式不同 - ASP.NET MVC handles 404 errors differently on local IIS and when deployed to Azure 在将IIS“嵌套”应用程序部署到主网站后,第二个ASP.NET Web应用程序挂起 - a second ASP.NET web app hangs after deployed as IIS 'nested' Application onto primary web site 如何检查IIS内部署的asp.net mvc Web应用程序的日志 - how to check the logs for my asp.net mvc web application which is deployed inside IIS 使用Asp.Net Web API AuthorizeAttribute时回复404 - Respond with 404 when using Asp.Net Web API AuthorizeAttribute 如何解决部署到本地文件的 asp.net web api 的 404 错误 - How to resolve a 404 error for a asp.net web api deployed to a local file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM