Tag: asp.net web api routing

使用媒体类型对ASP.NET Web API 2进行版本控制

我正在使用ASP.NET Web API 2进行属性路由,但我似乎无法使用媒体类型application/vnd.company[.version].param[+json]进行版本控制。 我收到以下错误: 给定的密​​钥不在字典中。 它源于在FindActionMatchRequiredRouteAndQueryParameters()方法中测试键_actionParameterNames[descriptor] 。 foreach (var candidate in candidatesFound) { HttpActionDescriptor descriptor = candidate.ActionDescriptor; if (IsSubset(_actionParameterNames[descriptor], candidate.CombinedParameterNames)) { matches.Add(candidate); } } 来源: ApiControllerActionSelector.cs 经过进一步调试后,我意识到如果你有两个控制器 [RoutePrefix(“api/people”)] public class PeopleController : BaseApiController { [Route(“”)] public HttpResponseMessage GetPeople() { } [Route(“identifier/{id}”)] public HttpResponseMessage GetPersonById() { } } [RoutePrefix(“api/people”)] public class PeopleV2Controller : BaseApiController { […]

为什么我的带有双args的Web API方法没有被调用?

我有一个Web API方法,需要两个双参数: 存储库界面: public interface IInventoryItemRepository { . . . IEnumerable GetDepartmentRange(double deptBegin, double deptEnd); . . . } 库: public IEnumerable GetDepartmentRange(double deptBegin, double deptEnd) { // Break the doubles into their component parts: int deptStartWhole = (int)Math.Truncate(deptBegin); int startFraction = (int)((deptBegin – deptStartWhole) * 100); int deptEndWhole = (int)Math.Truncate(deptEnd); int endFraction = (int)((deptBegin […]

FromBody字符串参数给出null

这可能是非常基本的东西,但我无法弄清楚我哪里出错了。 我试图从POST的主体中获取一个字符串,但“jsonString”只显示为null。 我也想避免使用模型,但也许这是不可能的。 我用PostMan打的那段代码是这个块: [Route(“Edit/Test”)] [HttpPost] public void Test(int id, [FromBody] string jsonString) { … } 也许这是我对邮递员做错的事情,但我一直试图在身体的价值部分使用“= test”(如在关于这个主题的其他问题中看到的那样) – x-www-form-urlencoded section with密钥作为jsonString而没有。 我也尝试过使用raw-text和raw-text / plain。 我得到了身份证,所以我知道url是正确的。 任何有关这方面的帮助将不胜感激。 PostMan目前设置如下: POST http://localhost:8000/Edit/Test?id=111 key = id value = 111 Body – x-www-form-urlencoded key = jsonString value = “=test”

多个控制器类型具有相同的路由前缀ASP.NET Web Api

是否可以将GET和POST分离为单独的API控制器类型并使用相同的路径前缀访问它们? 这是我的控制器: [RoutePrefix(“api/Books”)] public class BooksWriteController : EventStoreApiController { [Route(“”)] public void Post([FromBody] CommandWrapper commandWrapper){…} } [RoutePrefix(“api/Books”)] public class BooksReadController : MongoDbApiController { [Route(“”)] public Book[] Get() {…} [Route(“{id:int}”)] public Book Get(int id) {…} }