大佬教程收集整理的这篇文章主要介绍了ASP.NET Web API中具有多个GET方法的单个控制器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我发现支持额外的GET方法以及支持常规REST方法的最佳方法。将以下路由添加到WebAPIConfig:
routes.MaphttpRoute("DefaultAPIWithID", "API/{Controller}/{ID}", new { ID = RouteParameter.optional }, new { ID = @"\d+" });
routes.MaphttpRoute("DefaultAPIWithAction", "API/{Controller}/{action}");
routes.MaphttpRoute("DefaultAPIGet", "API/{Controller}", new { action = "Get" }, new { httpR_389_11845@ethod = new httpR_389_11845@ethodConsTraint(httpR_389_11845@ethod.Get) });
routes.MaphttpRoute("DefaultAPIPost", "API/{Controller}", new {action = "Post"}, new {httpR_389_11845@ethod = new httpR_389_11845@ethodConsTraint(httpR_389_11845@ethod.Post)});
我通过下面的测试类验证了此解决方案。我能够在下面的控制器中成功命中每种方法:
public class TESTController : APIController
{
public String Get()
{
return String.Empty;
}
public String Get(int id)
{
return String.Empty;
}
public String GetAll()
{
return String.Empty;
}
public voID Post([FromBody]String value)
{
}
public voID Put(int id, [FromBody]String value)
{
}
public voID delete(int id)
{
}
}
我确认它支持以下请求:
GET /Test
GET /Test/1
GET /Test/GetAll
POST /Test
PUT /Test/1
deletE /Test/1
注意 ,如果多余的GET操作不是以“ Get”开头,则可能需要向该方法添加httpGet属性。
在Web API中,我有一类类似的结构:
public class someController : ApiController
{
[WebGet(UriTemplate = "{itemsourcE}/Items")]
public SomeValue GetItems(CustomParam parameter) { ... }
[WebGet(UriTemplate = "{itemsourcE}/Items/{parent}")]
public SomeValue GetChildItems(CustomParam parameter,SomeObject parent) { ... }
}
由于我们可以映射各个方法,因此在正确的位置获得正确的请求非常简单。对于只有一个GET
方法但也有一个Object
参数的类似类,我成功使用IActionValueBinder
。但是,在上述情况下,出现以下错误:
@H_189_5@multiple actions were found that match the request: SomeValue GetItems(CustomParam parameter) on type SomeType SomeValue GetChildItems(CustomParam parameter,SomeObject parent) on type SomeType
我试图通过覆盖ExecuteAsync
方法来解决此问题,ApiController
但到目前为止还没有运气。关于这个问题有什么建议吗?
编辑:我忘了提一下,现在我正尝试在ASP.NET Web API上移动此代码,而ASP.NET Web
API具有不同的路由方法。问题是,如何使代码在ASP.NET Web API上工作?
以上是大佬教程为你收集整理的ASP.NET Web API中具有多个GET方法的单个控制器全部内容,希望文章能够帮你解决ASP.NET Web API中具有多个GET方法的单个控制器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。