且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

webapi控制器调用错误的方法

更新时间:2023-02-12 22:20:47

我在webApiConfig.js中配置了此

I had this configured in webApiConfig.js

在这里,我定义了唯一的路由,并带有一个称为id的可选参数. 我在我的GetItemById()方法中更改了参数名称,这可行.

here i have the sole route defined w/ an optional parameter called id. I changed the parameter name in my GetItemById() method and this works.

公共静态无效寄存器(HttpConfiguration配置) { //Web API配置和服务

public static void Register(HttpConfiguration config) { // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.EnableCors();
    
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

}


  [System.Web.Http.HttpGet]
        [Route("{id:int}")]
        public CustomJsonStringResult GetItemById([FromUri] int id){...}

我无法解释的是为什么非通用控制器能够按预期工作,而基于通用控制器却无法正常工作. 我确实相信现在情况可能更正确",只是希望我有一个事实的答案. 我的补丁方法也遇到了同样的问题.将参数重命名为id即可.

What I can not explain is why the non generic controller worked as expected but the generic-based controller does not. I do believe things are probably 'more correct' now, just wish I had a factual answer. I also ran into the same problem with my patch method. Renamed the parameter to id and it worked.

希望这可以帮助某人.