Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net WebAPI and AttributeRouting - no overloaded attribute constructor?

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'**
like image 698
jaffa Avatar asked Dec 16 '22 11:12

jaffa


2 Answers

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

like image 116
Kiran Avatar answered Jan 17 '23 16:01

Kiran


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}")]

like image 32
pat capozzi Avatar answered Jan 17 '23 16:01

pat capozzi