MVC 4 +控制器中接受不同参数名称的方法

时间:2013-11-28 09:10:38

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我是MVC的新手,目前我正在使用Postman来测试我的webapi方法功能。我意识到,如果我在路由中指定了我的方法中的不同参数,我将得到一个

"message": "The requested resource does not support http method 'GET'." error

邮递员。我想知道是否可以在控制器中使用不同的参数名称?谢谢。

    [ActionName("DefaultAction")]
// This method won't be callable because the argument name is different from the routing.
    public virtual IEnumerable<CaseComment> Get(int CaseId) 
    {
       // Implementation
    }

    [ActionName("DefaultAction")]
    public virtual HttpResponseMessage Post(int CaseCommentId)
    {
       // Implementation
    }

    Routing:

        config.Routes.MapHttpRoute(
         name: "CaseComment",
         routeTemplate: "api/v1/casecomment/{CaseCommentId}/{action}",
         defaults: new { controller = "casecomment", CaseCommentId= RouteParameter.Optional, action = "DefaultAction" }
         );

1 个答案:

答案 0 :(得分:0)

据我所知,你不能这样做,除非一个是[HttpGet],而另一个是[HttpPost]

相关问题