Search code examples
c#asp.net-core-webapiasp.net-web-api-routingself-host-webapi

Connecting to WebAPI2 endpoints(routes) throw Http code 503 for a controller and another controller works fine


I am using Owin self-hosting with WebAPI2. I have two controller classes and using attribute routing. One of them has following signature:

    [RoutePrefix("api/v1/devices")]
    public class DeviceController : ApiController
    {

        [HttpPost]
        [Route("")]
        public async Task<HttpResponseMessage> DevicePresence()
        {
           ...
        }

        [HttpGet]
        [Route("updates/{deviceID}")]
        public HttpResponseMessage GetDeviceUpdates(string deviceID)
        {
            ...
        }
    }

This one is working fine and action methods get triggered.

The other Controller has below signature:

    [RoutePrefix("device/class")]
    public class RemoteController : ApiController
    {
        [HttpGet]
        [Route("remotehost")]
        public HttpResponseMessage RemoteHost()
        {
            ...
        }

        [HttpGet]
        [Route("version")]
        public HttpResponseMessage GetVersion()
        {
            ...
        }
     }

When I try to connect to any of these I get 503 (Service Unavailable) response.

My Startup class is initialized as below:

    public class Startup
    {
        public static void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.EnsureInitialized();

            app.UseWebApi(config);
        }
    }

I tried to use the conventional routing as well but still the same problem.

config.Routes.MapHttpRoute(
                name: "RemoteApi",
                routeTemplate: "device/{controller}/{action}"
            );

public class ClassController : ApiController
{
       public HttpResponseMessage GetVersion()
        {
            ...
        }
}

This is also throwing 503 status code If I change the Route prefix in the second controller as below then it's working:

[RoutePrefix("api/v1/device/class")]
    public class RemoteController : ApiController
    {
      ...
    }

As per my requirement, I couldn't change the endpoints.

I am not sure what's wrong here and any help would be much appreciated.


Solution

  • I realized it was not a problem with webapi or routing. Actually, I was adding a firewall exception for the URL's and somehow it was not getting removed from firewall settings and keeps an entry in DACL. I removed this from cmd prompt and now everything works fine.

    Sorry for bothering...