0

I followed some answers but getting the following error on postman

{ "Message": "The requested resource does not support http method 'GET'." }

these were the links i followed

Web API route to action name

How to add custom methods to ASP.NET WebAPI controller?

currently setup is like the one as sky-dev has described

Custom method names in ASP.NET Web API

Updated with code

webapiconfig.cs

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: "DefaultApi",
        //    routeTemplate: "api/{controller}/{id}",
        //    defaults: new { id = RouteParameter.Optional }
        //);
        config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
        config.Routes.MapHttpRoute("DefaultApiWithAction", "api/{controller}/{action}");
        config.Routes.MapHttpRoute(name: "ApiWithActionName",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional });
        config.Routes.MapHttpRoute("DefaultApiGet", "api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
        config.Routes.MapHttpRoute("DefaultApiPost", "api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

    }

defaultcontroller.cs

public class DefaultController : ApiController
{
    DBEntities db = new DBEntities();
    public IEnumerable<CategoryDTO> Category()
    {
        IEnumerable<CategoryDTO> List = from tr in db.Categories
                                                  where tr.IsActive == "Y"
                                                  orderby tr.DisplayOrder ascending
                                                  select new CategoryDTO()
                                                  {
                                                      Id = tr.Id,
                                                      Name = tr.Name
                                                  };
        return List;
    }
    // GET: api/Default
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET: api/Default/5
    public string Get(int id)
    {
        return "value";
    }

    // POST: api/Default
    public void Post([FromBody]string value)
    {
    }

    // PUT: api/Default/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE: api/Default/5
    public void Delete(int id)
    {
    }


}
Community
  • 1
  • 1
Sracanis
  • 490
  • 5
  • 25

1 Answers1

0

If you are using web api 2, then you can use Attribute Routing Follow this url, I hope you will get your answer

Sample:

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

To know more detail follow this link http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

There is another way you can use custom methode by writing your own route in WebApiConfig.cs you can found this detail in this link http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Hope this will help you.

Riyadh Ul Islam
  • 172
  • 1
  • 7