ChatGPT解决这个技术问题 Extra ChatGPT

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

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>
I have the same issue. I have not yet found a solution, however one thing I have discovered is that if select the site in IIS, then go to the Handler Mappings feature, there is a mapping for static files which maps * to a file that must exist. When I remove this mapping and add a new mapping for all HTTP verbs, I no longer get the 404, it is replaced by a blank white page.
>>using the VS 2010 debugging dev server. -- AKA the evil Cassini. See blogs.msdn.com/b/rickandy/archive/2011/04/22/… -- If that doesn't work, create a new MVC 4 WebApi app and test deployment - simple

R
Roberto Leite de Moraes

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.


+1 for that one. But I changed it in the Handler Mappings on the Application from with IIS Manager. It was turned on for a bunch of verbs. I changed that to all verbs (*) and voila. But always better to put in the source.
I've the same problem, but these changes didn't help me. Is there any other configuration, too? Or may be a library reference? Please, also, see: stackoverflow.com/questions/27303523/…
many people say that using runAllManagedModulesForAllRequests will impact performance (check answers of hemant gautam below). However I cannot get the same service works, so I follow the configuration here: blog.maartenballiauw.be/post/2012/12/07/… That link also points out that enabling WebDAV can affect the result as well
Awesome answer!
For me, the verb was already *. I had to change the path to * as well to make it work as *. still caused the issue
h
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.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>

Works fine, but it's not very good solution. It's better to use UrlRoutingModule (see answer by Lopsided below). britishdeveloper.co.uk/2010/06/…
B
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.

For Windows 7 and earlier:

Run the Command Prompt (cmd.exe) as an administrator. Navigate to the appropriate .NET Framework location. (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) Run aspnet_regiis.exe -i

For Windows 8 and later:

From the start menu, type "Turn windows features on or off" and select the first result. 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). Click OK.


I migrated from IIS Express for development over to full IIS and this is what fixed it for me. Thanks!
Similar to @JimBrown above; it worked for me after migrating from IIS express.
This solved it for me to. On Windows 7, Visual Studio 2015 Ent, new MVC 5 website, changed from IIS Express to full IIS.
L
Leniel Maccaferri

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


hehehe... I had named my app in IIS as api too. This caused all this trial and error debugging for over 2 hours. Thank you very much for sharing your experience! Renamed it and now I'm back to business again. :D
Thanks - this was my issue! :)
I am not sure why the api calls were failing when i had hosted my project under a port 8080, just moving it as a virtual directory under the default website did the trick :)
C
Community

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.

Stack Overflow - Diagnosing 404 errors on IIS 7 and ASP.NET MVC

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.


Hit this problem on a 2008 (not R2) server, and this was the only solution that worked for me. Also, I had to combine this with setting the app pool to "integrated" mode.
J
Joe Schrag

A few things to check:

Make sure that you have the .NET Framework 4 installed. Ensure that version 4 of the .NET Framework is selected for your website & virtual directory (if applicable). Ensure that you have MVC installed or have the appropriate DLLs in your bin directory. Might need to allow ASP.NET 4.0 web service extensions Put the application in it's own app pool. Make sure the directory has at least "Scripts Only" execute permissions.


I have 4 other normal web applications running on the same IIS server and they are all using .net framework 4. so which of those 4 points aren't needed? when I published my mvc application I added the add deployable dependencies and added ASP.NET MVC so it is in my bin directory
@Armand Sounds like you've done #1. #2 is still necessary. Adding deployable dependencies, if you did it as described here: haacked.com/archive/2011/05/25/bin-deploying-asp-net-mvc-3.aspx, should take care of #3 above. #4 may or may not be necessary though I don't have the knowledge to tell you when it is & is not needed.
B
Ben Anderson

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

https://i.imgur.com/rSOwuYW.png


S
Sadish Kumar V

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


Those DLL's are required if you don't have MVC installed on the Server. In my case I was seeing a blank page when trying to invoke the API's. Adding the DLL's manually worked for me. Thanks!!
My problem was solved after adding System.Net.Http to the main publish folder, mine was Asp.net Core solution
C
Community

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

Thank you so much Greg, i was about to kill me because of this stupid path="*." but now, after dropping this miserable dot, everything works perfectly fine! Thank you very much!
c
coson

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.


I discovered this on my own, too. Upvoting your answer because it is easy to miss. When I created a site for my app, IIS automatically created an app pool for me, set to .NET v2.0 !! Why, why, why?? :)
P
Pakman

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


And where do you do that?
It's on a dialog that appears when you right-click the project and select Publish. It looks like this
R
Roman O

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.


Thank you so much! I tried absolutely everything else, and this was the only thing that fixed it.
Same here, thank you so much for adding this answer! Was the solution for my issue!
J
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.

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


This worked for me. I migrated an Azure application back to a VM deployment and after commenting out the contents of WebRole.cs, my WebAPI calls started working again.
I must have spent a day on this! Commenting WebRole.cs worked - wonder why however
S
Stephan Bauer

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>

t
toddmo

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.


Even i am also facing the same issue. and as you suggested i checked host name in http binding, and it updated properly only. But still my issue persists. Note: I hosted my API application as a child application. Please suggest if any one have idea on this. ex: "sample.example.com" is my main application and created a API under this domain as "sample.example.com/myAPI/"
A
Adem Aygun

Don't forget to deploy global.asax


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


a
adnan

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

Use Web Api in the same project using MVC or asp.net forms Use RouteConfig and WebApiConfig in Global.asax as GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); 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>

J
JuniorEbuka

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.


J
JohnH

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.


E
Emad Armoun

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.


O
Oliver Picton

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


C
Community

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.


b
bluish

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


M
Marian Siminescu

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.


g
guiomie

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.


m
miked

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


p
protango

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


K
K-Dawg

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>


A
Albert Marsnik

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.