locked
WebApiConfig doesn't raise custom method GET RRS feed

  • Question

  • User-425970132 posted

    Hi.

    I have the following code in ArticlesController

    // GET services/articles
            public JObject Get()
            {
                ...
            }
    
            // GET services/articles/5
            public JObject Get(int id)
            {
                ...
            }
    
            //GET /services/articles/stores/2
            [System.Web.Http.HttpGet]
            public JObject GetArticlesByStoreId(string id)
            {
                ...
            }
    
            // POST services/articles
            public void Post([FromBody]Articles value)
            {
                ...
            }

    This is the code in WebApiConfig

    // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "services/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    
                config.Routes.MapHttpRoute(
                    name: "GetArticlesByStoreIdApi",
                    routeTemplate: "services/{controller}/stores/{id}",
                    defaults: new { action = "GetArticlesByStoreId", id = RouteParameter.Optional }
                );

    When I go to /services/articles/1 it throws "Multiple actions were found that match the request"

    How can I do to execute /services/articles/1 and /services/articles/stores/2 without raising the error.

    Thank you so much.

    Monday, July 11, 2016 3:22 PM

Answers

  • User36583972 posted

    Hi diegomauricio987,

    From your description, you have some wrong in webconfig and get methods.

    How can I do to execute /services/articles/1 and /services/articles/stores/2 without raising the error

    You can use attribute routing in ASP.NET Web API. The following code for your reference.

    ArticlesController:

      // GET services/articles
            [Route("services/articles")]
            public JObject Get()
            {
                
            }
    
            // GET services/articles/5
            [Route("services/articles/{id}")]
            public JObject Get(int id)
            {
               
            }
    
            //GET /services/articles/stores/2
            [Route("services/articles/stores/{id}")]
            [System.Web.Http.HttpGet]
            public JObject GetArticlesByStoreId(string id)
            {
                
            }
    
            // POST services/articles
            [Route("services/articles")]
            public void Post([FromBody]Articles value)
            {
                
            }
    

    WebApiConfig:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Attribute routing.
            config.MapHttpAttributeRoutes();
    
            // Convention-based routing.
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
    

    Attribute Routing in ASP.NET Web API 2:

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

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 1:46 AM

All replies

  • User36583972 posted

    Hi diegomauricio987,

    From your description, you have some wrong in webconfig and get methods.

    How can I do to execute /services/articles/1 and /services/articles/stores/2 without raising the error

    You can use attribute routing in ASP.NET Web API. The following code for your reference.

    ArticlesController:

      // GET services/articles
            [Route("services/articles")]
            public JObject Get()
            {
                
            }
    
            // GET services/articles/5
            [Route("services/articles/{id}")]
            public JObject Get(int id)
            {
               
            }
    
            //GET /services/articles/stores/2
            [Route("services/articles/stores/{id}")]
            [System.Web.Http.HttpGet]
            public JObject GetArticlesByStoreId(string id)
            {
                
            }
    
            // POST services/articles
            [Route("services/articles")]
            public void Post([FromBody]Articles value)
            {
                
            }
    

    WebApiConfig:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Attribute routing.
            config.MapHttpAttributeRoutes();
    
            // Convention-based routing.
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
    

    Attribute Routing in ASP.NET Web API 2:

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

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 1:46 AM
  • User-425970132 posted

    Thank you very much Yohann. I am a beginner in WebApi, I spend all day without getting a result. It works perfectly :)

    Tuesday, July 12, 2016 3:42 AM