ASP.NET Web API 中具有多个 GET 方法的单个控制器 [英] Single controller with multiple GET methods in ASP.NET Web API

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

问题描述

在 Web 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 方法但还有一个 Object 参数的类似类,我成功地使用了 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

我试图通过覆盖 ApiControllerExecuteAsync 方法来解决这个问题,但到目前为止还没有运气.关于这个问题有什么建议吗?

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 上移动此代码,该 API 具有不同的路由方法.问题是,如何使代码在 ASP.NET Web API 上工作?

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 方法和支持普通 REST 方法的最佳方式.将以下路由添加到您的 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 操作不以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天全站免登陆