在网页API控制器的多HttpPost方法 [英] Multiple HttpPost method in Web API controller

查看:167
本文介绍了在网页API控制器的多HttpPost方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用MVC4的Web API项目,我有多个 HttpPost 方法控制器。控制器类似如下:

控制器

 公共类VTRoutingController:ApiController
{
    [HttpPost]
    公共MyResult路线(MyRequestTemplate routingRequestTemplate)
    {
        返回null;
    }    [HttpPost]
    公共MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
    {
        返回null;
    }
}

这里 MyRequestTemplate 重新presents模板类负责处理的Json通过请求来了。

错误:

当我提出使用招为 HTTP请求://本地主机:52370 / API / VTRouting / TSPRoute 的http://本地主机: 52370 / API / VTRouting /路由我得到一个错误:


  

多重行动中发现符合要求,即


如果我删除它工作正常,上面的方法之一。

的Global.asax

我曾尝试在修改的Global.asax 默认的路由表,但我仍然得到错误,我想我在Global.asax中定义路由有问题。以下是我在Global.asax中正在做的。

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.MapHttpRoute(
            名称:MyTSPRoute
            routeTemplate:API / VTRouting / TSPRoute
            默认:新{}
      );    routes.MapHttpRoute(
        名称:MyRoute
        routeTemplate:API / VTRouting /路由
        默认:新{行动=路由}
    );
}

我正在使用POST在提琴手的要求,通过JSON在RequestBody为MyRequestTemplate。


解决方案

您可以在单个控制器多个动作。

有关,你必须做以下两件事情。


  • 首先装点像 ActionName 属性动作 [ActionName(路径)]


 公共类VTRoutingController:ApiController
{
  [ActionName(路径)]
  公共MyResult布线后(MyRequestTemplate routingRequestTemplate)
  {
     返回null;
  }  [ActionName(tspRoute)]
  公共MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate)
  {
     返回null;
  }
}



  • 第二个定义 WebApiConfig 文件下列途径。


  //控制器仅
//为了处理像`/ API / VTRouting`路线
config.Routes.MapHttpRoute(
    名称:ControllerOnly
    routeTemplate:API / {}控制器
);//与ID控制器
//为了处理像`/ API / VTRouting / 1`路线
config.Routes.MapHttpRoute(
    名称:ControllerAndId
    routeTemplate:API / {}控制器/(编号),
    默认值:空,
    限制:新{ID = @^ \\ D + $} //只有整数
);//与动作控制器
//为了处理像`/ API / VTRouting / route`路线
config.Routes.MapHttpRoute(
    名称:ControllerAndAction
    routeTemplate:API / {控制器} / {行动}
);


I am starting to use MVC4 Web API project, I have controller with multiple HttpPost methods. The Controller looks like the following:

Controller

public class VTRoutingController : ApiController
{
    [HttpPost]
    public MyResult Route(MyRequestTemplate routingRequestTemplate)
    {
        return null;
    }

    [HttpPost]
    public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
    {
        return null;
    }
}

Here MyRequestTemplate represents the template class responsible for handling the Json coming through the request.

Error:

When I make a request using Fiddler for http://localhost:52370/api/VTRouting/TSPRoute or http://localhost:52370/api/VTRouting/Route I get an error:

Multiple actions were found that match the request

If I remove one of the above method it works fine.

Global.asax

I have tried modifying the default routing table in global.asax, but I am still getting the error, I think I have problem in defining routes in global.asax. Here is what I am doing in global.asax.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapHttpRoute(
            name: "MyTSPRoute",
            routeTemplate: "api/VTRouting/TSPRoute",
            defaults: new { }
      );

    routes.MapHttpRoute(
        name: "MyRoute",
        routeTemplate: "api/VTRouting/Route",
        defaults: new {action="Route" }
    );
}

I am making the request in Fiddler using POST, passing json in RequestBody for MyRequestTemplate.

解决方案

You can have multiple actions in a single controller.

For that you have to do the following two things.

  • First decorate actions with ActionName attribute like [ActionName("route")]

public class VTRoutingController : ApiController
{
  [ActionName("route")]
  public MyResult PostRoute(MyRequestTemplate routingRequestTemplate)
  {
     return null;
  }

  [ActionName("tspRoute")]
  public MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate)
  {
     return null;
  }
}

  • Second define the following routes in WebApiConfig file.

// Controller Only
// To handle routes like `/api/VTRouting`
config.Routes.MapHttpRoute(
    name: "ControllerOnly",
    routeTemplate: "api/{controller}"             
);

// Controller with ID
// To handle routes like `/api/VTRouting/1`
config.Routes.MapHttpRoute(
    name: "ControllerAndId",
    routeTemplate: "api/{controller}/{id}",
    defaults: null,
    constraints: new { id = @"^\d+$" } // Only integers 
);

// Controllers with Actions
// To handle routes like `/api/VTRouting/route`
config.Routes.MapHttpRoute(
    name: "ControllerAndAction",
    routeTemplate: "api/{controller}/{action}"
);

这篇关于在网页API控制器的多HttpPost方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆