Search code examples
c#model-view-controllerasp.net-mvc-routingself-host-webapi

Routing issue in self hosted webapi


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?


Solution

  • Change route

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