locked
can we change web api action name by attribute routing RRS feed

  • Question

  • User264732274 posted

    please tell me can we change web api action name by attribute routing without using action name attribute ?

    i know we can use action name attribute to change action name. it is used to over load action.

    thanks

    Wednesday, November 30, 2016 1:58 PM

Answers

  • User-1672470423 posted

    You can use RoutePrefix with Route to acheive above:

    Example:

    [RoutePrefix("api/Services")]
        public class ServicesController : ApiController
        {
            [AcceptVerbs("GET", "POST")]
            [HttpGet]
            [Route("SomeMethod")]
            public string[] SomeMethod()
            {
                return new string[] { "String1", "String2", "String3" };
            }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2016 2:38 PM
  • User-2057865890 posted

    Hi Sudip_inn,

    You simply add an attribute to the controller action:

    [Route("customers/{customerId}/orders")]
    public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

    To enable attribute routing, call MapHttpAttributeRoutes during configuration.

    using System.Web.Http;
    
    namespace WebApplication
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                // Other Web API configuration not shown.
            }
        }
    }

    reference:

    https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    http://www.c-sharpcorner.com/uploadfile/b1df45/web-api-route-and-route-prefix-part-2/ 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 1, 2016 9:00 AM
  • User-1672470423 posted

    Based on the example mentioned by Chris to access GetOrdersByCustomer. It will be like below:

    http://YourServiceBaseURL/api/customers/{customerId}/orders //{customerId} is the id of customer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 1, 2016 3:07 PM

All replies

  • User-1672470423 posted

    You can use RoutePrefix with Route to acheive above:

    Example:

    [RoutePrefix("api/Services")]
        public class ServicesController : ApiController
        {
            [AcceptVerbs("GET", "POST")]
            [HttpGet]
            [Route("SomeMethod")]
            public string[] SomeMethod()
            {
                return new string[] { "String1", "String2", "String3" };
            }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2016 2:38 PM
  • User-2057865890 posted

    Hi Sudip_inn,

    You simply add an attribute to the controller action:

    [Route("customers/{customerId}/orders")]
    public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

    To enable attribute routing, call MapHttpAttributeRoutes during configuration.

    using System.Web.Http;
    
    namespace WebApplication
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                // Other Web API configuration not shown.
            }
        }
    }

    reference:

    https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    http://www.c-sharpcorner.com/uploadfile/b1df45/web-api-route-and-route-prefix-part-2/ 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 1, 2016 9:00 AM
  • User264732274 posted

    so tell me how the url would look like for the action GetOrdersByCustomer ?

    thanks

    Thursday, December 1, 2016 3:02 PM
  • User-1672470423 posted

    Based on the example mentioned by Chris to access GetOrdersByCustomer. It will be like below:

    http://YourServiceBaseURL/api/customers/{customerId}/orders //{customerId} is the id of customer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 1, 2016 3:07 PM