c# - Can you have 2 GET methods with different parameter types within the same web api controller?

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:asp.net mvc4 web api 资料

[第一个 Web API 程序 从零开始创建 Web API 项目地址如下:http://www.cnblogs.com/beginor/archive/2012/03/19/2406624.htmlweb api的一系列文章,方便以后查看http://www

This question already has an answer here:

I have an asp.net web api controller with 2 GET methods in it. One accepts a string parameter and the other accepts an int parameter. I only have the default route that comes set up with web api in place.

        public HttpResponseMessage GetSearchResults(string searchTerm)
        {
            HttpResponseMessage response;
            //Do Work
            return response;
        }

        public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage response;
            //Do Work
            return response;
        }

Every time I pass an int value in the URL, the GET method that takes the string parameter is called. The GET method that takes the int parameter is never called.

Is it possible to have 2 GET methods with different parameter types within the same controller?

-Edit- The suggested duplicate question is different because it asks about 2 methods with the exact same parameter types - I'm asking about different parameter types.

c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api2
|
  this question
edited Dec 14 '15 at 17:13 asked Dec 14 '15 at 17:04 Aaron 654 1 13 26

marked as duplicate by Orel Eraki, Yvette Colomb, Anik Islam Abhi, MZaragoza, JiTHiN Dec 15 '15 at 4:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



 | 

1 Answers
1

---解决方法---解决方法---

Yes, it is possible. Out of the box with the default configuration, what you have should work assuming you are passing the searchTerm as a query string parameter. However, if you are attempting to pass it as part of the URL, like for example /api/myurl/blah, the default convention routing will attempt to match it with the int version of the method and return an error. You will either have to edit the default configuration or use Attribute Routing.

In general I find the convention based MVC routing to be less useful in WebApi so I usually disable it and use Attribute Routing.

推荐:asp.net web api

[官方的地址:http://www.asp.net/web-api里面的资料很全,是学习的第一选择.建议多参考此资料1.里面最简单直接的例子http://www.asp.net/web-api/overview/getting-started-

To enable attribute routing, add

config.MapHttpAttributeRoutes();

to your WebApi config.

Then you can mark up your methods as such

[HttpGet]
[Route("api/myobject/")]
public HttpResponseMessage GetSearchResults(string searchTerm)
{
    HttpResponseMessage response;
    //Do Work
    return response;
}

[HttpGet]
[Route("api/myobject/{id:int}")]
public HttpResponseMessage Get(int id)
{
    HttpResponseMessage response;
    //Do Work
    return response;
}

Now, you can call the first method via

/api/myobject?searchTerm=blah

and the second via

/api/myobject/1

and they shouldn't collide.

However, if you want to have the searchTerm be in the URL instead of the query parameters, you can change the route to

[Route("api/myobject/{searchTerm}")]

The api/myobject/{id:int} route will catch all the ids and the api/myobject/{searchTerm} will catch most anything else. However, be careful with this as if the URL isn't URL encoded weird things will tend to happen.

I don't know precisely what URL formatting you are looking for so what I provided are just simple examples. The link I posted earlier delves deeper into attribute routing. It allows you to make for more complex routes than routing by convention that WebApi inherited from MVC.


|
  this answer
edited Dec 14 '15 at 17:48 answered Dec 14 '15 at 17:20 Bradford Dillon 869 4 13      Excellent explanation - attributed routing worked perfectly! –  Aaron Dec 14 '15 at 22:02      No problem. Glad it helped. –  Bradford Dillon Dec 15 '15 at 0:48

 | 

Not the answer you're looking for? Browse other questions tagged c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api2 or ask your own question.

推荐:ASP.NET Web Api 教程

[使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页摘要: 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html前言这篇文章我们将使

相关推荐