处理ASP.NET Web API多个GET方法单控制器 [英] Single controller with multiple GET methods in ASP.NET Web API

查看:438
本文介绍了处理ASP.NET Web API多个GET方法单控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页API我有一个类结构相似的:

In Web API I had a class of similar structure:

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 方法,但也有一个对象参数,我成功地使用 IActionValueBinder 。然而,在案件如上所述,我得到了以下错误:

Since we could map individual methods, it was very simple to get the right request at the right place. For similar class which had only a single GET method but also had an Object parameter, I successfully used IActionValueBinder. However, in the case described above I get the following error:

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 方法,但至今没有运气来解决这个问题。在这个问题上有什么建议?

I am trying to approach this problem by overriding the ExecuteAsync method of ApiController but with no luck so far. Any advice on this issue?

编辑:我忘了提,我现在想继续前进的ASP.NET Web API,它有不同的方法来路由这个code。现在的问题是,我该如何作出的ASP.NET Web API的code的工作?

I forgot to mention that now I am trying to move this code on ASP.NET Web API which has a different approach to routing. The question is, how do I make the code work on ASP.NET Web API?

推荐答案

这是我发现的支持额外的GET方法和支持正常的休息方法,以及最好的方式。以下路线添加到您的WebApiConfig:

This is the best way I have found to support extra GET methods and support the normal REST methods as well. Add the following routes to your 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 { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new {action = "Post"}, new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});

我验证与下面的测试类这一解决方案。我能成功击中每种方法在下面我的控制器:

I verified this solution with the test class below. I was able to successfully hit each method in my controller below:

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)
    {
    }
}

我验证,它支持以下要求:

I verified that it supports the following requests:

GET /Test
GET /Test/1
GET /Test/GetAll
POST /Test
PUT /Test/1
DELETE /Test/1

注意的,如果你的额外的GET操作不首先'获取'你可能需要添加一个HTTPGET属性的方法。

Note That if your extra GET actions do not begin with 'Get' you may want to add an HttpGet attribute to the method.

这篇关于处理ASP.NET Web API多个GET方法单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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