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

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

这是我的 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>
262700 次浏览

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.

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

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

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 URL routing as follows:

Original: localhost:3092/api/values
Moved: 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.

Additional note: I had further complicated the issue by adding api as the alias of an application within my website which caused the effective URL to be:

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

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.asax for the Web API "DefaultAPI" from api/{controller}/{id} to {controller}/{id} and the ASP.NET MVC one Default from {controller}/{id} to info/{controller}/{id}.

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

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

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

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.

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

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.

For Windows 7 and earlier:

  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

For Windows 8 and later:

  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.

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.html above 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.

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

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.

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

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

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:

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

system.webServer tag was already there of course but I added the modules tag to it and then the remove & add tags to modules tag.

I recently had a 404 not found error with all my Web Api 2 routes/controllers. So I went onto the actual server and tried to browse using localhost instead of the hostname and got "404.7 Not Found - The request filtering module is configured to deny the file extension".

This SO post help me solve it.

I had a similar problem. I had the right settings in my web.config file but was running the application pool in Classic mode rather than Integrated mode

screen shot

This issue can also happen due to the following

1.In the 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

  • System.Net.Http

  • System.Net.Http.Formatting

  • System.Web.Http.WebHost

  • System.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. 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

Sadish Kumar.V

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

I had to disable the File Publish Option "Precompile during publishing."

I had the same issue: on a newly installed machine with Visual Studio 2013, the web api project was working under IISExpress, but not under local IIS. I tried everything I could find, but in the end the problem was not necessary with Web API, but with MVC: even it was installed, no MVC project was running.

What worked for me was to uninstall IIS (from ADD/REMOVE Windows Features), then reinstall it, and then run aspnet_regiis -i. Maybe this helps somebody else.

I spent lot's of time trying a lot of things to finally realise I was adding my web app not in Sites/Default Web Sites, but in another website binded to another port. Obviously trying localhost on port 80 would give a 404.

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>

i do nothing, just add this tag in web.config, its working this issue come up one of the following points

  1. Use Web Api in the same project using MVC or asp.net forms

  2. Use RouteConfig and WebApiConfig in Global.asax as GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes);

  3. Use RouteConfig for 2 purposes, asp.net forms using with friendlyurl and mvc routing for MVC routing

we just use this tag in web.config, it will work.

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

In my case, the issue was simply that I was trying to access the site at

myserver.myintranet.com/mysite

But the web site binding for http in IIS didn't have the host name specified in the binding. It had worked before and I have no idea how that got blown away.

Once I put myserver.myintranet.com into the host name the 404 was gone.

In IIS Manager you go into Bindings... in the actions pane, then edit the http binding to specify the host name.

Based on this SO answer, I just had to change path="*." to path="*" for the added ExtensionlessUrlHandler-Integrated-4.0 in configuration>system.WebServer>handlers in my web.config

Before:

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

After:

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

I also ran into this problem. I solved the problem by going to the Application Pools > Application Pool Name and changed the .NET Framework from version v.2.0.50727 to v4.0.30319.

There is official fix from microsoft: http://support.microsoft.com/kb/980368

I strongly do NOT recommend to use < modules runAllManagedModulesForAllRequests="true">. This leads all requests (even .jpg, .css, .pdf, etc) will be processed by all registered HTTP modules. There are two negative moments: a) additional load on hardware resources; b) potential errors, as http modules will process new type of content.

Don't forget to deploy global.asax

Ran into the same issue with Web API and .Net Core Web API. Worked fine in VS 2017 while debugging, but returned 404 when published to IIS 7.5. Solution for me was to change the way I created the site. Instead of publishing to the root of a Web Site (created by right clicking Sites...Add Web Site), I had to create an Application (created by right clicking a Web Site...Add Application) and publish to that folder. Note that for the Core version, I had to change the Application Pool .NET Framework Version setting to "No Managed Code".

I struggled with this, as well. My exact issue was that I had an ASMX Web Service that, when I entered a parameter into a web method and tested it, then it would give me the 404. The particular method had worked fine in the past and hadn't been changed, only re-published. Then I got here and tried all of the posted answers & nothing helped.

My ultimate solution? I know this is drastic, but I just created a new Visual Studio solution and web project. Selected MVC, then I did an "Add" > "New Item", selected "Visual C#" > "Web" and "Web Service (ASMX)" under that. I copied all of my old code-behind code, then I took note of the namespace it gave the new file in my new project, then pasted all of my old code into the new code-behind file in the new project and put the namespace back to what it had been.

Then I created my folders in my project that I had before using Visual Studio to do "Add" > "New Folder", then copied back in my files into the folders from my other project using Windows Explorer, then right-clicked each folder in Visual Studio and did "Add" > "Existing Item..." and pulled the items in those folders into my new project's Visual Studio folders. I referenced all my .NET assemblies again, having both projects open so I could compare which ones I had referenced, previously (there were several). I had to name my new project slightly different - basically I did something comparable to "GeneralWebApp" instead of "MyWebApp", for example - so I had to do a "Replace All" in my whole solution to replace that name, so it would get the right namespace for all my files.

Then I did a "Rebuild All" on the project, then started it up with the "Play" button Visual Studio gives when I got it to build correctly. It worked fine. So I published it, and everything was fine on the server where I published it, when I ran it from there. I have no explanation as to what happened, but that's how I got through it. It's not a bad test just to see if something Visual Studio is doing has mucked it up.

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.

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

Ensure that not selected Solution -> Project -> Right click Properties -> Application -> Auto-generate binding redirects

Try this webconfg.. replace "NewsApi.dll" with your main dll!


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\NewsApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
</configuration>


If you place only the bin folder in IIS (after building the project), this problem will be occurred too. In this situation you should publish the project using VisualStudio, then place the published folder into IIS.

This is a really obvious answer/rookie mistake, but I thought I would just put it here in case it might help someone. So my structure was that a had a Website in IIS with the frontend as one application underneath the website and the backend was the other. The backend application had six seperate api applications sitting beneath it. I had forgotten to convert each of the api folders to applications within IIS and of course this was causing the routes to my api endpoints to return a 404.0 error.

My Point: Check the simple stuff first! Make sure that you have converted all folders to applications in IIS where that is required for your website to function correctly.

I had a similar issue because I did not define routing correctly for web api. Here is my solution:

public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = System.Web.Http.RouteParameter.Optional } );

    }

These pages helped me:

https://learn.microsoft.com/en-us/previous-versions/aspnet/hh834746(v=vs.118)?redirectedfrom=MSDN

https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api

None of the above worked for me. Finally it wasn't anything related to any configuration.

I was getting the 404 error while trying to run a debug session through Visual Studio 2017 using the built in IISExpress 10.0. For me it was working when the release was published to the IIS server but getting a 404 when debugging locally through VS.

The issue turned out to be some old compiled files lying in the project bin directory which appeared to be interfering. Finally did a "Clean solution", closed VS, deleted the bin and obj folder. Restarted VS and rebuild project and the error went away.