0

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.

2 Answers2

0

Your RemoteController throws an exception because you do not follow naming conventions of WEB API. Your RemoteHost() method which is a get method has to have a "Get" prefix, so its name is supposed to actually be GetRemoteHost(). That should solve it.

In case you want to alter the naming conventions, you can modify route definitions in the global.asax file:

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

Hope that helped.

Avi Meltser
  • 409
  • 4
  • 11
  • Though I was using attribute routing so action name shouldn't be a problem: But I tried to only use GetVersion() and commented out the other action method but still the same problem. I have updated the question with what I tried in conventional routing. – Vikas Rayiparambil May 17 '19 at 03:19
0

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