4

我刚刚下载了用于 WebAPI 的 AttributeRouting NuGet 包,并且在我的控制器中出现了问题。我认为使用它的方法是有类似的东西:

 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();
            }
}

但是我收到以下错误:错误 2 'System.Web.Http.HttpGetAttribute' 不包含采用 1 个参数的构造函数

我需要尽快启动并运行它。HttpGet 属性没有重载构造函数有什么原因吗?

更新

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

在路线:

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

当我点击 'api/inbox/EnquiryCount' 的 URL 时,我收到此错误:

**No HTTP resource was found that matches the request URI 'http://localhost:49597/api/inbox/enquirycount'**
4

2 回答 2

6

Web api 2 支持属性路由

以下是详细信息:http ://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

于 2013-10-21T17:06:46.950 回答
6

此语法在较新版本的 webapi 中已更改。[HTTPPOST] 现在是独立的,并且路由的新属性恰当地命名为 ROUTE,它采用路由 url,例如。

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

于 2014-05-29T02:11:15.997 回答