4

I have just downloaded AttributeRouting NuGet package for WebAPI and having a problem within my controller. I thought the way to use it was to have something like:

 public class InboxController : ApiController
    {
        private IInboxService _inboxService;

        public InboxController(IInboxService inboxService)
        {
            _inboxService = inboxService;            
        }

        public IEnumerable<MessageModel> GetAll()
        {
            return _inboxService.GetAllMessages();
        }

      [HttpGet("Inbox/Count")]
            public int GetInboxCount()
            {
                return _inboxService.GetMessageCount();
            }
}

However I get the following error: Error 2 'System.Web.Http.HttpGetAttribute' does not contain a constructor that takes 1 arguments

I need to get this up and running fairly quickly. Is there any reason why the HttpGet attribute doesn't have an overloaded constructor?

UPDATE

    [GET("Inbox/EnquiryCount")]
    public EnquiryCountModel GetEnquiryCounts()
    {
        var model = new EnquiryCountModel();
        model.EnquiryCount = _inboxService.GetCustomerEnquiriesCount();
        model.EnquiryResponseCount = _inboxService.GetCustomerEnquiryResponseCount();
        return model;
    }

In routes:

routes.MapHttpRoute("InboxEnquiryApi", "api/inbox/{action}", new { Controller = "Inbox" }, null, new WebApiAuthenticationHandler(GlobalConfiguration.Configuration));

When I hit the URL at 'api/inbox/EnquiryCount' I get the this error:

**No HTTP resource was found that matches the request URI 'http://localhost:49597/api/inbox/enquirycount'**
10
  • It's GETAttribute instead of HttpGet Commented May 9, 2013 at 5:41
  • GetAttribute is not resolved when compiled, are you sure? Commented May 9, 2013 at 6:00
  • Yup, GETAttribute. Add using AttributeRouting.Web.Http; Commented May 9, 2013 at 6:06
  • GET works of course;) but am now getting multiple Multiple Actions Were Found that match the request error. I thought this was the whole point of this library. Can they not be mixed? Commented May 9, 2013 at 6:18
  • You need to understand how the ASP.NET Web API routing works internally. It works with url as a resource in mind. AttributeRouting just add a route, similar to global.asax routes. It cannot change the underlying engine. asp.net/web-api/overview/web-api-routing-and-actions Commented May 9, 2013 at 6:22

2 Answers 2

6

Attribute routing is supported in Web api 2

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

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

Comments

6

This syntax has been changed in newer versions of the webapi. The [HTTPPOST] is now standalone and there is a new attribute for the route aptly name ROUTE which takes the route url eg.

[Route("GetRes/{month}")]

Comments

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.