Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API application gives 404 when deployed at IIS 7

I have an ASP.NET Web API which works fine when running on "IIS Express" with localhost:1783

Settings in VS

But when I uncross the "Use IIS Express" and then press "Create Virtual Directory"...

New settings

...I just get 404 errors: The 404 error

Any ideas whats wrong? Thanks!

like image 948
Cotten Avatar asked Mar 13 '13 15:03

Cotten


People also ask

How do I fix 404 error in IIS?

Resolution. To resolve this problem, verify that the file requested in the browser's URL exists on the IIS computer and that it is in the correct location. Use the IIS Microsoft Management Console (MMC) snap-in to determine where the file requested must exist in the IIS computer's file system.

How do I deploy Web API in IIS 7?

Go to IIS and right-click on the application then select Manage Application -> Browse. It will look as in the image shown below. The preceding Blue screen shows that the Web API is up and running. Since the WebApi is already in running mode, I'll execute the following link to add an employee to the database.

How does Web API handle 404 error?

A simple solution is to check for the HTTP status code 404 in the response. If found, you can redirect the control to a page that exists. The following code snippet illustrates how you can write the necessary code in the Configure method of the Startup class to redirect to the home page if a 404 error has occurred.


2 Answers

While the marked answer gets it working, all you really need to add to the webconfig is:

    <handlers>       <!-- Your other remove tags-->       <remove name="UrlRoutingModule-4.0"/>       <!-- Your other add tags-->       <add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition=""/>     </handlers> 

Note that none of those have a particular order, though you want your removes before your adds.

The reason that we end up getting a 404 is because the Url Routing Module only kicks in for the root of the website in IIS. By adding the module to this application's config, we're having the module to run under this application's path (your subdirectory path), and the routing module kicks in.

like image 143
DavidAndroidDev Avatar answered Sep 22 '22 12:09

DavidAndroidDev


For me, in addition to having runAllManagedModulesForAllRequests="true" I also had to edit the "path" attribute below. Previously my path attribute was "*." which means it only executed on url's containing a dot character. However, my application's url's don't contain a dot. When I switched path to "*" then it worked. Here's what I have now:

  <system.webServer>       <validation validateIntegratedModeConfiguration="false" />       <modules runAllManagedModulesForAllRequests="true">       <remove name="WebDAVModule"/>       </modules>        <handlers>           <remove name="WebDAV" />           <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />           <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />           <remove name="ExtensionlessUrlHandler-Integrated-4.0" />           <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />           <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />           <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />       </handlers>   </system.webServer> 
like image 25
thebiggestlebowski Avatar answered Sep 23 '22 12:09

thebiggestlebowski