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

C#asp.net Web-ApiIis 7.5Http Status-Code-404

C# Problem Overview


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>

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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>

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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

Solution 8 - C#

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

Solution 9 - C#

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

Solution 10 - C#

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.

Solution 11 - C#

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

Solution 12 - C#

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.

Solution 13 - C#

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.

Solution 14 - C#

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>

Solution 15 - C#

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.

Solution 16 - C#

Don't forget to deploy global.asax

Solution 17 - C#

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.

Solution 18 - C#

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.

Solution 19 - C#

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.

Solution 20 - C#

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.

Solution 21 - C#

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

Solution 22 - C#

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>

Solution 23 - C#

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.

Solution 24 - C#

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

Solution 25 - C#

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.

Solution 26 - C#

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.

Solution 27 - C#

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>

Solution 28 - C#

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

Solution 29 - C#

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

Solution 30 - C#

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>

Solution 31 - C#

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.

Solution 32 - C#

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://docs.microsoft.com/en-us/previous-versions/aspnet/hh834746(v=vs.118)?redirectedfrom=MSDN

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

Solution 33 - C#

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.

Solution 34 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArmandView Question on Stackoverflow
Solution 1 - C#Kevin OrtmanView Answer on Stackoverflow
Solution 2 - C#hemant gautamView Answer on Stackoverflow
Solution 3 - C#Brandon GanoView Answer on Stackoverflow
Solution 4 - C#Nicholas BargerView Answer on Stackoverflow
Solution 5 - C#Ross BrasseauxView Answer on Stackoverflow
Solution 6 - C#Joe SchragView Answer on Stackoverflow
Solution 7 - C#Rob SedgwickView Answer on Stackoverflow
Solution 8 - C#Sadish Kumar VView Answer on Stackoverflow
Solution 9 - C#GregView Answer on Stackoverflow
Solution 10 - C#cosonView Answer on Stackoverflow
Solution 11 - C#PakmanView Answer on Stackoverflow
Solution 12 - C#Roman OView Answer on Stackoverflow
Solution 13 - C#Josh MouchView Answer on Stackoverflow
Solution 14 - C#RakeshView Answer on Stackoverflow
Solution 15 - C#toddmoView Answer on Stackoverflow
Solution 16 - C#Adem AygunView Answer on Stackoverflow
Solution 17 - C#devilciusView Answer on Stackoverflow
Solution 18 - C#vapcguyView Answer on Stackoverflow
Solution 19 - C#JuniorEbukaView Answer on Stackoverflow
Solution 20 - C#Emad ArmounView Answer on Stackoverflow
Solution 21 - C#Oliver PictonView Answer on Stackoverflow
Solution 22 - C#Konstantin IsaevView Answer on Stackoverflow
Solution 23 - C#santosView Answer on Stackoverflow
Solution 24 - C#Anilkumar YView Answer on Stackoverflow
Solution 25 - C#Marian SiminescuView Answer on Stackoverflow
Solution 26 - C#guiomieView Answer on Stackoverflow
Solution 27 - C#adnanView Answer on Stackoverflow
Solution 28 - C#mikedView Answer on Stackoverflow
Solution 29 - C#protangoView Answer on Stackoverflow
Solution 30 - C#K-DawgView Answer on Stackoverflow
Solution 31 - C#Albert MarsnikView Answer on Stackoverflow
Solution 32 - C#Leonid MinkovView Answer on Stackoverflow
Solution 33 - C#rboyView Answer on Stackoverflow
Solution 34 - C#Zübeyir KoçalioğluView Answer on Stackoverflow