调用我的 WebAPI 方法时的 HTTP 404



我正在尝试使用提琴手调用下面定义的WebAPI方法,但是我遇到了以下异常。请让我知道我在这里缺少什么,因为我无法点击定义的方法。

方法原型:

[Route("api/tournament/{tournamentId}/{matchId}/{teamId}/{userEmail}/PostMatchBet")]
public HttpResponseMessage PostMatchBet(int tournamentId, int matchId, int teamId, string userEmail)

该方法在锦标赛 WebAPI 控制器中定义,并尝试访问将 HTTP 谓词设置为发布的方法,如下所示,

http://localhost:59707/api/tournament/PostMatchBet?tournamentId=1&matchId=1&teamId=8&userEmail=abc@gmail.com

请让我知道我在这里错过了什么。

异常详细信息:"消息

详细信息":"在 上找不到任何操作 与请求匹配的控制器'锦标赛'。

确保路由配置正确完成。

public static class WebApiConfig 
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();
        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

确保动作具有正确的动词和路由。您正在混合基于约定的路由和属性路由。决定你想使用哪一个并坚持下去。

要使此 POST URL 正常工作...

http://localhost:59707/api/tournament/PostMatchBet?tournamentId=1&matchId=1&teamId=8&userEmail=abc@gmail.com

它将匹配基于约定的路由模板,例如

api/{controller}/{action}

映射到此操作。

public class TournamentController : ApiController {
    [HttpPost]
    public HttpResponseMessage PostMatchBet(int tournamentId, int matchId, int teamId, string userEmail) { ... }
}

如果执行属性路由,则需要将控制器更新为...

[RoutePrefix("api/tournament")]
public class TournamentController : ApiController {
    //POST api/tournament/postmatchbet{? matching query strings}
    [HttpPost]
    [Route("PostMatchBet")]
    public HttpResponseMessage PostMatchBet(int tournamentId, int matchId, int teamId, string userEmail) { ... }
}

在您的网址路径中

http://localhost:59707/api/tournament/PostMatchBet?tournamentId=1&matchId=1&teamId=8&userEmail=abc@gmail.com
[Route("api/tournament/{tournamentId}/{matchId}/{teamId}/{userEmail}/PostMatchBet")]

如您所见,您应该根据您设置的路由以该方法结束。您还说您希望通过斜杠进行分离,而不是作为参数化 URL,例如 (?, &(。因此,如果您传入这样的东西,它将收听它。

http://localhost:59707/api/tournament/1/1/8/abc@gmail.com/PostMatchBet

相关内容

最新更新