1

In a self hosted webapi console application project, I am not able to hit the SayHello method using http://localhost:9998/api/shri/flows/config.

Error:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:9998/api/shri/flows/config'.",
    "MessageDetail": "No route data was found for this request."
}

Controller:

    class ConfigController : ApiController
    {
        [HttpGet, Route("{id}/flows/config")]
        public string SayHello([FromUri] string id)
        {
            return "Hello " + id;
        }
    }

Startup:

    public class Startup
    {
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWebApi(config);
        }
    }

To keep the self hosted webapi running I have the following:

    public class Program
    {
        static void Main()
        {
            Uri myUri = new Uri(@"http://localhost:9998/");
            // Let have our configurations readY
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri);

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

            HttpSelfHostServer server = new HttpSelfHostServer(config);

            // Start listening 
            server.OpenAsync().Wait();

            Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be tested now");
            Console.ReadLine();

        }
    }

What am I missing?

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57

3 Answers3

4

Change route

[HttpGet,Route("api/{id}/flows/config")]

Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19
2

With such routing your action will be accessible by http://localhost:9998/shri/flows/config url (without /api part)

If you want to access that action with http://localhost:9998/api/shri/flows/config url either correct Route attribute for the action:

[HttpGet, Route("api/{id}/flows/config")]
public string SayHello([FromUri] string id)

or add RoutePrefix attribute on controller class:

[RoutePrefix("api")]
public class ConfigController : ApiController
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
0

For someone in the future that might be struggling with this, as Nkosi said you need to enable attribute routing.

 public class Program
    {
        static void Main()
        {
            Uri myUri = new Uri(@"http://localhost:9998/");
            // Let have our configurations readY
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri);

            // enables the attribute routing
            config.MapHttpAttributeRoutes();

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

            HttpSelfHostServer server = new HttpSelfHostServer(config);

            // Start listening 
            server.OpenAsync().Wait();

            Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be 
            tested now");
            Console.ReadLine();

        }
    }

If attribute routing is not configured then it does not matter if your routing is correct, When the request is made to the API it will not get the parameter values from the address itself even if you use [FromBody] or [FromUri].

I hope this helps someone, It was the issue in my case.