Home > Software engineering >  Simple controller routing doesn't work in IIS for asp.net site hosted in IIS
Simple controller routing doesn't work in IIS for asp.net site hosted in IIS

Time:11-23

Trying to host locally a rest service in IIS. Everything works fine in IIS Express but as soon as I try to host locally in IIS the routing doesn't work.

  1. It's compiled to x86 and is hosted inside of an app pool that allows for 32 bit processes.
  2. I have the site mapped to the binary output folder from my build msbuild /p:Configuration=Release /p:ProcessorArchitecture=x86 -r:False
  3. I have Anonymous Authentication enabled
  4. Binding is type http to port 8000 enter image description here
  5. I have enabled directory browsing and see the below at http://localhost:8000/ enter image description here
  6. However whenever I try to navigate to http://localhost:8000/api/HeartBeat I get a 404 - Not Found.

My WebApiConfig class which sets a default route.

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

Here is the controller

public class HeartBeatController : ApiController
    {
        // GET: api/HearBeat/5
        public HttpResponseMessage Get()
        {
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

    }

If I run this in IIS Express and navigate to that same url I get a 200. Also I've tried to examine the URL in powershell using Get-WebUrl and it shows a ProtocolError but I have no clue what or why. And IIS Manager doesn't seem to complain about anything.

PS C:\Windows\system32> Get-WebURL -PSPath "IIS:\Sites\Test" | Format-list


Status      : ProtocolError
ResponseUri : http://localhost:8000/
Description : Forbidden

CodePudding user response:

A couple of things were amiss.

  1. Publishing via Visual Studio is quite different than the build output that was created via the build command above.
  2. I was missing under Windows features/IIS/Application Development Features ![enter image description here
  3. Finally was still getting a security error which required adding an ACL rule to the directory
  • Related