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)
    {
    }


}
6
  • did you decorate it with [HttpGet] ? Commented Jul 13, 2016 at 15:10
  • Yeah i did that and its working with that but i have seen in one of the project that it wasn't decorated with [HttpGet] but it was working.. Commented Jul 13, 2016 at 15:14
  • need to see your code to debug this. Commented Jul 13, 2016 at 15:16
  • I recommend this article exceptionnotfound.net/… in the middle of it, the author explains how ASP.NET finds the default method for an action Commented Jul 13, 2016 at 15:20
  • Could you post the URL you entered to access your API? Commented Jul 13, 2016 at 16:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Riyadh.. thanks for your suggestion i have one through the article but is it possible to use custom methods without decoration?
Thanks man! you made my day.. Now i felt much more command on routing.. thumbs up!
I am happy that, my ans helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.