无法在 WebApi 2 中为 Get 配置路由
Unable to configure route for Get in WebApi 2
我正在为一些非常基本的事情而苦苦挣扎。我正在尝试从我的 WebApi2 restful 服务获得响应,但我做不到。
我没有编辑默认的 WebApi (WebApiConfig.cs
) 路由。
这是控制器
public class AboutController
{
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int accountId, string mainservice, string secondaryservice)
{
//logic
}
}
如果我(在浏览器中)导航到 http://localhost:58090/api/about
,我会收到错误消息 The requested resource does not support http method 'GET'.
我想这是有道理的,因为它与路线(路径)不匹配。
如果我将路径更新为与签名匹配的内容,例如 http://localhost:58090/api/about/1/a/b
,我会收到错误消息 No action was found on the controller About' that matches the request.
即使我将 [HttpGet]
添加到 controller
,也没有什么区别。
作为健全性测试,我更新为
public class AboutController
{
public async Task<IHttpActionResult> Get()
{
//logic
}
}
它做了预期的事情。我不知道为什么添加参数会使事情变得如此混乱。
我不知道自己做错了什么
路由必须匹配参数
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int id, string mainService, string secondaryService)
{
上面的方法不起作用,因为它希望根据路由看到 service1 和 service2。
按照下面的例子更新
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int id, string service1, string service2)
{
我正在为一些非常基本的事情而苦苦挣扎。我正在尝试从我的 WebApi2 restful 服务获得响应,但我做不到。
我没有编辑默认的 WebApi (WebApiConfig.cs
) 路由。
这是控制器
public class AboutController
{
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int accountId, string mainservice, string secondaryservice)
{
//logic
}
}
如果我(在浏览器中)导航到 http://localhost:58090/api/about
,我会收到错误消息 The requested resource does not support http method 'GET'.
我想这是有道理的,因为它与路线(路径)不匹配。
如果我将路径更新为与签名匹配的内容,例如 http://localhost:58090/api/about/1/a/b
,我会收到错误消息 No action was found on the controller About' that matches the request.
即使我将 [HttpGet]
添加到 controller
,也没有什么区别。
作为健全性测试,我更新为
public class AboutController
{
public async Task<IHttpActionResult> Get()
{
//logic
}
}
它做了预期的事情。我不知道为什么添加参数会使事情变得如此混乱。
我不知道自己做错了什么
路由必须匹配参数
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int id, string mainService, string secondaryService)
{
上面的方法不起作用,因为它希望根据路由看到 service1 和 service2。
按照下面的例子更新
[Route("api/about/{id:int}/{service1}/{service2}")]
public async Task<IHttpActionResult> Get(int id, string service1, string service2)
{