我刚刚为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'中的网址时,我收到此错误:
**No HTTP resource was found that matches the request URI 'http://localhost:49597/api/inbox/enquirycount'**
答案 0 :(得分:6)
Web api 2
支持属性路由以下是详细信息:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
答案 1 :(得分:6)
此语法在较新版本的webapi中已更改。 [HTTPPOST]现在是独立的,路由的新属性恰当地命名为ROUTE,它采用路由URL 例如
〔路线(&#34; GetRes / {月}&#34)]