Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 未遵守Webapi v1路由_Asp.net Mvc 4_Asp.net Web Api_Ninject - Fatal编程技术网

Asp.net mvc 4 未遵守Webapi v1路由

Asp.net mvc 4 未遵守Webapi v1路由,asp.net-mvc-4,asp.net-web-api,ninject,Asp.net Mvc 4,Asp.net Web Api,Ninject,webapi调用返回错误: message: "No HTTP resource was found that matches the request URI 'http://localhost:25396/api/dmpe/form/123'.", messageDetail: "No action was found on the controller 'Dmpe' that matches the request." 控制器: [HttpGet] [ActionNa

webapi调用返回错误:

  message: "No HTTP resource was found that matches the request URI 'http://localhost:25396/api/dmpe/form/123'.",
  messageDetail: "No action was found on the controller 'Dmpe' that matches the request."
控制器:

   [HttpGet]
    [ActionName("GetForm")]
    public HttpResponseMessage GetForm(long formId)
    {
        try
        {
            var form = this.dmpeService.GetForm(formId);
            return Request.CreateResponse(HttpStatusCode.OK, form);
        }
        catch (Exception e)
        {
            var serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(new { success = false, message = e.Message });
            _logger.Error("GetForm Error: {0}", e.Message);

            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, json);
        }

    }      
WebApi路由配置:

   //config.Routes.MapHttpRoute(
   //         name: "GetForm",
   //         routeTemplate: "api/dmpe/form/{id}",
   //         defaults: new
   //         {
   //             controller = "Dmpe",
   //             action = "GetForm"
   //         }
   //     );
更新路线:

  config.Routes.MapHttpRoute(
            name: "GetForm",
            routeTemplate: "api/{controller}/form/{id}"
        );
我根据目前的命名约定确定了路线。这条路行得通

   config.Routes.MapHttpRoute(
            name: "GetMenu",
            routeTemplate: "api/forms/menu/{userTypeId}/{ctyhocn}",
            defaults: new
            {
                controller = "Service",
                action = "GetMenu"
            },
            constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Get.ToString() }) }
        );      
现在在WebApi v2中,我已经习惯于在控制器中定义路由,如下所示,但这是我正在了解的v2特性。根据当前的示例,我原以为routeTemplate可以工作。我查看了我定义的路由的RouteTables.Routes,它的格式似乎是正确的:api/{controller}/form/{id}

WebApi v2路由属性

 [HttpGet]
 [AllowAnonymous]
 [Route("api/office/companynames/{searchValue}/")]
 .....

您收到的错误消息与路由的配置有关,而与Ninject无关。WebAPI 1.0或2.2的良好实践是使用以下模板:

routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );
与MVC不同,没有包含任何操作名称


所以,试着使用设置好的路线,看看是否可以让它工作。

@Karen\u B为我指出了正确的方向,但我想提供在其他人遇到这种情况时应用的更改

我将路由映射更改为:

        config.Routes.MapHttpRoute(
            name: "NewForm",
            routeTemplate: "api/{controller}/form/",
            defaults: new
            {
                controller = "Dmpe"
            },
            constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Post }) } 
        );

        config.Routes.MapHttpRoute(
            name: "GetForm",
            routeTemplate: "api/{controller}/form/{id}",
            defaults: new
            {
                controller = "Dmpe",
                id = RouteParameter.Optional
            },
            constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Get }) } 
        );
然后将控制器更改为:

    [HttpPost]
    public HttpResponseMessage Post(DmpeForm form)
    {
       ....


    [HttpGet]
    public HttpResponseMessage Get(long Id)
    {
    .....

将http方法属性添加到方法中可能是多余的,因为我更改了方法名称以反映http方法,但此时我只希望它能够工作。如果您有选择,请使用WebApi 2:-

Hello@Karen_B我正在遵循应用程序中当前的routeTemplate约定。例如,如果我将[FromBody]添加到控制器操作中,则这是应用程序中的一个工作路由。使用routeTemplate…有点…因为它没有获得我在url/api/dmpe/form/123中传递的实际id。您尝试过[FromUri]吗?不幸的是,WebAPI v1不像2那样具有路由属性。然而,我确实偶然发现了一个库,一些人编写该库是为了在v1中使用路由属性。对不起,我没有链接了。不用担心@Karen_B,我是在你的指导下找到的。v2用路由属性破坏了我。谢谢@WillLopez我相信您现在已经解决了这个问题,但我想传递一个资源,用于将属性路由添加到WebAPI 1。我昨天刚刚在我的项目中实现了这一点,它似乎工作得很好。它处理我们在WebAPI2.2中使用的大多数特性,但有一些语法差异。另外,{id:guid}不受支持,但仅使用{id}似乎是可行的。NuGet:AttributeRouting ASP.NET WebAPI。感谢您发布最终解决方案。我相信社区中的其他人会觉得这很有帮助。