C# 在 IIS 7.5 中托管的 Web Api 中找不到 HTTP 404 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9703090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:28:36  来源:igfitidea点击:

HTTP 404 Page Not Found in Web Api hosted in IIS 7.5

c#asp.net-web-apiiis-7.5http-status-code-404

提问by Armand

I have a Web Api application. It works perfectly well when I tested it using the VS 2010 debugging dev server. But I now deployed it to IIS 7.5 and I am getting a HTTP 404 error when trying to access the application.

我有一个 Web Api 应用程序。当我使用 VS 2010 调试开发服务器对其进行测试时,它运行良好。但是我现在将它部署到 IIS 7.5 并且在尝试访问应用程序时出现 HTTP 404 错误。

Here is my web.config

这是我的 web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-FlowGearProxy-20123141219;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

采纳答案by Kevin Ortman

I was struggling with this as well. Fortunately, Steve Michelotti documented a solution that worked for me here.

我也为此苦苦挣扎。幸运的是,Steve Michelotti在这里记录了一个对我有用的解决方案。

At the end of the day, I enabled all verbs (verb="*") to the ExtensionlessUrlHandler-Integrated-4.0 handler in my web config.

在一天结束时,我在 Web 配置中为 ExtensionlessUrlHandler-Integrated-4.0 处理程序启用了所有动词(动词 =“*”)。

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

Others have pointed out that having WebDAV enabled causes issues. Fortunately, I did not run into that issue as well.

其他人指出启用 WebDAV 会导致问题。幸运的是,我也没有遇到这个问题。

回答by Joe Schrag

A few things to check:

需要检查的几件事:

  1. Make sure that you have the .NET Framework 4 installed.
  2. Ensure that version 4 of the .NET Framework is selected for your website & virtual directory (if applicable).
  3. Ensure that you have MVC installed or have the appropriate DLLs in your bin directory.
  4. Might need to allow ASP.NET 4.0 web service extensions
  5. Put the application in it's own app pool.
  6. Make sure the directory has at least "Scripts Only" execute permissions.
  1. 确保您已安装 .NET Framework 4。
  2. 确保为您的网站和虚拟目录(如果适用)选择了 .NET Framework 版本 4。
  3. 确保您已安装 MVC 或在您的 bin 目录中有适当的 DLL。
  4. 可能需要允许 ASP.NET 4.0 Web 服务扩展
  5. 将应用程序放在它自己的应用程序池中。
  6. 确保目录至少具有“仅脚本”执行权限。

回答by Oliver Picton

What kind of HTTP request are you making?

你在做什么类型的 HTTP 请求?

This is a slightly left-field answer but have you tried removing the IIS default error page for 404 to check what your API is actually returning?

这是一个稍微偏左的答案,但您是否尝试删除 404 的 IIS 默认错误页面以检查您的 API 实际返回的内容?

I had an issue whereby I wanted a controller method to return a 404 when I POSTed the wrong id to it. I found that I was always getting the IIS 404 "File or directory not found" page rather than the HTTP response from my API. Removing the default 404 error page resolved the problem.

我有一个问题,我希望控制器方法在我向其发布错误的 ID 时返回 404。我发现我总是从我的 API 获得 IIS 404“找不到文件或目录”页面,而不是 HTTP 响应。删除默认的 404 错误页面解决了问题。

Different issue but you never know it may help ;)

不同的问题,但你永远不知道它可能有帮助;)

回答by Nicholas Barger

Are you running the Web API app in a virtual directory or an application?

您是在虚拟目录还是应用程序中运行 Web API 应用程序?

For example: I had the same issue when I moved my project to my local IIS under the Default Web Site > SampleWebAPI. I believe this is due to the change in the URLrouting as follows:

例如:当我将项目移动到默认网站 > SampleWebAPI 下的本地 IIS 时,我遇到了同样的问题。我相信这是由于URL路由的变化如下:

Original: localhost:3092/api/values
Moved: localhost/SampleWebAPI/api/values

原文: localhost:3092/api/values
移动: localhost/SampleWebAPI/api/values

If you move the Web API project to it's own website running on a different port it seems to work.

如果您将 Web API 项目移动到它自己在不同端口上运行的网站,它似乎可以工作。

Additional note: I had further complicated the issue by adding apias the alias of an application within my website which caused the effective URLto be:

附加说明:我通过api在我的网站中添加应用程序的别名使问题进一步复杂化,这导致有效URL

localhost:81/api/api/values- noticed this after moving the website to it's own website

localhost:81/api/api/values- 将网站移至自己的网站后注意到这一点

Therefore, because I wanted to maintain a separation between my website and the web api mvc project site, I changed the routing rules in global.asaxfor the Web API "DefaultAPI" from api/{controller}/{id}to {controller}/{id}and the ASP.NET MVC one Defaultfrom {controller}/{id}to info/{controller}/{id}.

因此,因为我想保持我的网站和 web api mvc 项目站点之间的分离,我global.asax将 Web API“DefaultAPI”的路由规则从api/{controller}/{id}to{controller}/{id}和 ASP.NET MVC 一个Default{controller}/{id}to info/{controller}/{id}

回答by Josh Mouch

I started getting 404 responses from Web API after following a Windows Azure tutorial that told me to add a file "WebRole.cs" to my project.

在遵循 Windows Azure 教程后,我开始从 Web API 获得 404 响应,该教程告诉我将文件“WebRole.cs”添加到我的项目中。

After removing "WebRole.cs" from my project, my Web API calls started working again.

从我的项目中删除“WebRole.cs”后,我的 Web API 调用又开始工作了。

回答by Brandon Gano

If IIS is installed or enabled after ASP.NET, you will need to manually register ASP.NET with IIS in order for your .NET application to work.

如果在 ASP.NET 之后安装或启用了 IIS,您将需要手动向 IIS 注册 ASP.NET,以便您的 .NET 应用程序工作。

For Windows 7 and earlier:

对于 Windows 7 及更早版本:

  1. Run the Command Prompt (cmd.exe) as an administrator.
  2. Navigate to the appropriate .NET Framework location. (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319)
  3. Run aspnet_regiis.exe -i
  1. 以管理员身份运行命令提示符 (cmd.exe)。
  2. 导航到相应的 .NET Framework 位置。(例如 C:\Windows\Microsoft.NET\Framework64\v4.0.30319)
  3. 运行aspnet_regiis.exe -i

For Windows 8 and later:

对于 Windows 8 及更高版本:

  1. From the start menu, type "Turn windows features on or off" and select the first result.
  2. Expand Internet Information Services: World Wide Web Services: Application Development Features and select ASP.NET 4.5 (or ASP.NET 3.5 if you need to support projects on .NET Framework 2.0-3.5).
  3. Click OK.
  1. 从开始菜单中,键入“打开或关闭 Windows 功能”并选择第一个结果。
  2. 展开 Internet 信息服务:万维网服务:应用程序开发功能并选择 ASP.NET 4.5(如果需要支持 .NET Framework 2.0-3.5 上的项目,则选择 ASP.NET 3.5)。
  3. 单击确定。

回答by hemant gautam

Had same issue. This configuration setting solved the issue.

有同样的问题。此配置设置解决了该问题。

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

As explained in http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.htmlabove solution should be avoided. Use this instead. Same solution is provided by Lopsided also. Keeping it here to let users avoid implementing the first working solution.

http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html 中所述,应避免使用上述解决方案。改用这个。Lopside 也提供了相同的解决方案。将其保留在这里是为了让用户避免实施第一个可行的解决方案。

<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->
</modules>

回答by devilcius

Had the same issue, a 404 response for the web api controllers when served from the IIS but everything worked fine from VS2010. None of the above solutions worked for me. Eventually I found that the problem was that we added WSE 3.0 support for the application and the Microsoft.Web.Services3 dll was missing in the application's /bin directory. Weird, but after copying the dll, the route mapping started to work.

有同样的问题,当从 IIS 提供服务时,web api 控制器的 404 响应,但在 VS2010 中一切正常。以上解决方案都不适合我。最终我发现问题在于我们为应用程序添加了 WSE 3.0 支持,而应用程序的 /bin 目录中缺少 Microsoft.Web.Services3 dll。奇怪,但是复制dll后,路由映射开始工作。

回答by Konstantin Isaev

This piece of configuration in web.config file can help as helped to me: in the system.webServer section:

web.config 文件中的这段配置对我有帮助:在 system.webServer 部分:

      <security>
          <requestFiltering>
              <verbs applyToWebDAV="true">
                  <remove verb="PUT" />
                  <add verb="PUT" allowed="true" />
                  <remove verb="DELETE" />
                  <add verb="DELETE" allowed="true" />
                  <remove verb="PATCH" />
                  <add verb="PATCH" allowed="true" />
              </verbs>
          </requestFiltering>
      </security>      

回答by Lopsided

This is the only answer that worked for me...

这是唯一对我有用的答案......

I had a similar issue... It seemed like no matter what I did, nothing was getting redirected and my global file was just being ignored. I seriously considered just ending it all before I found this answer. I hope this link helps somebody else.

我有一个类似的问题......似乎无论我做什么,都没有重定向,我的全局文件只是被忽略了。在我找到这个答案之前,我认真考虑过结束这一切。我希望这个链接可以帮助其他人。



Adding the following to the web.config file worked for me:

将以下内容添加到 web.config 文件对我有用:

<system.webServer>
  <modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  </modules>
</system.webServer>

system.webServertag was already there of course but I added the modulestag to it and then the remove& addtags to modules tag.

system.webServer标签当然已经存在,但我添加了模块标签,然后删除添加标签到模块标签。