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

2020-01-24 06:38发布

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.

Here is my 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>

30条回答
别忘想泡老子
2楼-- · 2020-01-24 07:22

For me the problem was the root site was configured to use a .NET 2.0 app pool, and my application within that site was .NET 4.5.

I created a new site with a .NET 4 app pool and placed my application at the root of that - and that worked fine.

查看更多
唯我独甜
3楼-- · 2020-01-24 07:22

For me the solution was removing the following lines from my web.config file:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />
</dependentAssembly>

I noticed that VS had added them automatically, not sure why

查看更多
放我归山
4楼-- · 2020-01-24 07:22

Please make sure the application pool is in Integrated mode
And add the following to web.config file:

<system.webServer>
    .....
    <modules runAllManagedModulesForAllRequests="true" />
    .....
</system.webServer>
查看更多
唯我独甜
5楼-- · 2020-01-24 07:23

What kind of HTTP request are you making?

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?

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.

Different issue but you never know it may help ;)

查看更多
Explosion°爆炸
6楼-- · 2020-01-24 07:23

Don't forget to deploy global.asax

查看更多
祖国的老花朵
7楼-- · 2020-01-24 07:24

It got resolved for me, when I enable checkbox for UrlRoutingModule-4.0:

IIS Manager > Modules > select UrlRoutingModule-4.0 > Edit Module > check the check-box "Invoke only for requests to ASP.NET applications or managed handlers".

查看更多
登录 后发表回答