Differentiating Get Method Containing Query String in ASP.NET WEB API


I have a ASP.NET Web Api Containing following Get Methods:

[RoutePrefix("api/contact")] public class ContactController : ApiController { private ContactContext db = new ContactContext(); public IEnumerable<Contact> Get() { return db.Contacts.AsEnumerable(); } [Route("{id:int}")] public Contact Get(int id) { var result = db.Contacts.Find(id); if (result == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return result; } public IEnumerable<Contact> GetContacts([FromUri]Filter filter) { return db.Contacts.Where(c => c.DepartmentId == filter.DepartmentId && c.City == filter.City).ToList(); } }

In function public IEnumerable<Contact> GetContactsfilter([FromUri]Filter filter) DepartementId & City are in Query String like


My Question is how do I add Attribute Routing for Query,because Currently it cannot differentiate between the Get Methods and is returning error

{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nSystem.Collections.Generic.IEnumerable`1[MyFirstWebApi.Models.Contact] Get() on type MyFirstWebApi.Controllers.ContactController\r\nSystem.Collections.Generic.IEnumerable`1[MyFirstWebApi.Models.Contact] GetContacts(MyFirstWebApi.Models.Filter) on type MyFirstWebApi.Controllers.ContactController","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"}

I tried adding the Routing Attribute to Get method containing id but that doesn't worked.


Attribute routing or regular routing does not support query parameters in the templates. Instead you could modify your controller to just have two actions as below:

// Here if no DepartmentId or City are provided, then the 'filter' parameter would be null [Route] public IEnumerable<Contact> GetContacts([FromUri]Filter filter) [Route("{id:int}")] public Contact Get(int id)


  • Visual Studio mouse over debugging doesn't work for particular variable
  • StructureMap configuration for DI with asp.net MVC / Linq2Sql combo?
  • ASP NET attribute routing array
  • Pass jagged/multidimensional array to web api method
  • Jersey filters in non-servlet container
  • Authentication in webAPI
  • Is it correct to use Post instead of Get to fetch data in Web API
  • How to pass a pdf from WebAPI and read the pdf from the MVC Controller?
  • Generic interface problem
  • DELETE verb working in Postman but not with ajax
  • How to send a custom object from OnException method?
  • Html Example Response with Swagger and Swashbuckle
  • Azure Functions - can't be invoked from Azure WebJobs SDK
  • Add new records to private Outlook distribution list
  • CodeIgniter - strange output issue
  • WebElement.clear() fires javascript change event - Alternatives?
  • WCF Rest Error Handling
  • PayPal API adaptive pay Authentication failed. API credentials are incorrect
  • ASP.NET MVC ActionResult View() not changing url
  • Selenium ChromeDriver (C#) Crashes Only in Visual Studio Debug Mode
  • How can I detect if a URL is redirected to another one?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • dc-js disable selecting slices on click for pie chart
  • Spring security and special characters
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Obtain ObjectIdHex value from mgo query
  • Check if a string to interpolate provides expected placeholders
  • How to check if every primary key value is being referenced as foreign key in another table
  • ILMerge & Keep Assembly Name
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Why winpcap requires both .lib and .dll to run?
  • retrieve vertices with no linked edge in arangodb
  • Acquiring multiple attributes from .xml file in c#
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Python/Django TangoWithDjango Models and Databases