ASP.NET MVC Web API路由:找到与请求匹配的多个操作

时间:2016-08-27 02:58:30

标签: asp.net-mvc asp.net-web-api asp.net-web-api2 asp.net-web-api-routing

假设我在下面的代码中有以下两个控制器,它们有两条不同的路径:



 [RoutePrefix("api/reportDelivery")]
    public class ReportDeliveryController : ApiController
    {

        /// <summary>
        /// called as: http://localhost:1607/api/ReportDelivery/invoice?id=3
        
        [Route("Invoice/{id?}")]
        public Models.Report GetDeliveredInvoice(int id)
        {
           
        }
        [Route("Invoices/{id?}")]
        public Models.Report GetDeliveredInvoices(int id)
        {
          

        }
&#13;
&#13;
&#13;

我需要将每个api称为:

  1. http://localhost:1607/api/ReportDelivery/invoice?id=3
  2. http://localhost:1607/api/ReportDelivery/invoices?id=3
  3. 我用过&#34; s&#34;说明名称的差异。但是,它给了我一个错误&#34;找到了匹配请求的多个动作&#34;。 这是WebApiConfig:

    &#13;
    &#13;
     public static void Register(HttpConfiguration config)
            {
                config.EnableCors();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    &#13;
    &#13;
    &#13;

    的Global.asax:

    &#13;
    &#13;
     public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
               
            }
        }
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

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

必须定义完整路径。

控制器 行动 参数

你没有定义行动段。