locked
attribute routing is possible with self host? RRS feed

  • Question

  • User264732274 posted
    I have plan to host my api in windows service but i do not know attribute routing will work when web api will be hosted in win service.

    If i need to follow any special steps for this then plzz guide me. I search google for article on web api attribute routing with self host but found none.
    Saturday, August 6, 2016 8:20 PM

Answers

  • User36583972 posted


    Hi  sudip_inn,

    attribute routing is possible with self host

    Yes. You can refer the following code.

    1: Startup.cs

       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)
            {
                appBuilder.Use((context, next) =>
                {
                    context.Response.Headers.Remove("Server");
                    return next.Invoke();
                });
                appBuilder.UseStageMarker(PipelineStage.PostAcquireState);
    
                // Configure Web API for self-host. 
                HttpConfiguration config = new HttpConfiguration();
                //// Web API routes
                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );   
                appBuilder.UseWebApi(config);
            }
    
        }
    

    2: ValuesController.cs:

     [RoutePrefix("api/Values")]
        public class ValuesController : ApiController
        {
            // GET api/values 
            [Route("TestGet1")]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/TestGet2/5 
            [Route("TestGet2")]
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values 
            [Route("")]
            public void Post([FromBody]string value)
            {
            }
    
            // PUT api/values/5 
            [Route("")]
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5 
            [Route("")]
            public void Delete(int id)
            {
            }
        }

    3: Program.cs:

        class Program
        {
            static void Main(string[] args)
            {
                string baseAddress = "http://localhost:9000/";
    
                // Start OWIN host 
                using (WebApp.Start<Startup>(url: baseAddress))
                {
                    // Create HttpCient and make a request to api/values 
                    HttpClient client = new HttpClient();
                    HttpResponseMessage response = client.GetAsync(baseAddress + "api/Values/TestGet1").Result;
                    response.Headers.Remove("Server");
                    Console.WriteLine(response);
                    Console.WriteLine(response.Content.ReadAsStringAsync
                        ().Result);
                    Console.ReadLine();
                }
               
            }
        }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 2:10 AM

All replies

  • User36583972 posted


    Hi  sudip_inn,

    attribute routing is possible with self host

    Yes. You can refer the following code.

    1: Startup.cs

       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)
            {
                appBuilder.Use((context, next) =>
                {
                    context.Response.Headers.Remove("Server");
                    return next.Invoke();
                });
                appBuilder.UseStageMarker(PipelineStage.PostAcquireState);
    
                // Configure Web API for self-host. 
                HttpConfiguration config = new HttpConfiguration();
                //// Web API routes
                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );   
                appBuilder.UseWebApi(config);
            }
    
        }
    

    2: ValuesController.cs:

     [RoutePrefix("api/Values")]
        public class ValuesController : ApiController
        {
            // GET api/values 
            [Route("TestGet1")]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/TestGet2/5 
            [Route("TestGet2")]
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values 
            [Route("")]
            public void Post([FromBody]string value)
            {
            }
    
            // PUT api/values/5 
            [Route("")]
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5 
            [Route("")]
            public void Delete(int id)
            {
            }
        }

    3: Program.cs:

        class Program
        {
            static void Main(string[] args)
            {
                string baseAddress = "http://localhost:9000/";
    
                // Start OWIN host 
                using (WebApp.Start<Startup>(url: baseAddress))
                {
                    // Create HttpCient and make a request to api/values 
                    HttpClient client = new HttpClient();
                    HttpResponseMessage response = client.GetAsync(baseAddress + "api/Values/TestGet1").Result;
                    response.Headers.Remove("Server");
                    Console.WriteLine(response);
                    Console.WriteLine(response.Content.ReadAsStringAsync
                        ().Result);
                    Console.ReadLine();
                }
               
            }
        }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 2:10 AM
  • User264732274 posted

    thanks for your nice code but i have plan not to use OWIN with self host. when i will not use then how could i turn on attribute routing. please discuss with code example. thanks

    Monday, August 8, 2016 11:54 AM