locked
how to open method in rest web servics RRS feed

  • Question

  • User-297906461 posted

    hi

    i tray to add new method in my rest web service but when i test it it give me error

    it doesn't work  when i add parameter but when i don't add parameter it work !

    this is my code 

     [HttpGet]
            [ActionName("rrrr")]
    
            public String GetCDEF()
            {
                return "ddddddd";
            }
     
            [HttpGet]
            [ActionName("rrrr")]
            public string GetRetriveData(int s)
            {
                s = s * 3;
                return s.ToString();
    }

    and the apiconfig

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
                // Configure Web API to use only bearer token authentication.
                config.SuppressDefaultHostAuthentication();
                config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
    
                // Web API routes
                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                  name: "ActionApi",
                  routeTemplate: "api/{controller}/{Action}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }

    Monday, November 21, 2016 9:26 AM

All replies

  • User-1734649046 posted

    Hi,

    Please change variable from "s" to "id" in GetRetriveData as follows

    [HttpGet]
    [ActionName("rrrr")]
    public string GetRetriveData(int? id)
    {
        id = id * 3;
        return id.ToString();
    }

    This is required because in route configuration "id" parameter specified as follows

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


     

    Friday, November 25, 2016 4:12 PM