I have only worked with a few Restful API's and have been in need of creating one. Ultimately I need an API and was hoping for the Restful aspect but that portion isn't the ultimate focus.
I created a prototype and had the basic html calls working fine. I then wanted to move to other types of calls. I've looked at a lot of articles and snippets but none of them seem to really answer the question for me. I'm using Postman to test. Right now I have two basic calls "Get" calls both with three string parameters realizing they have the same signature but different names. I get an error related to ambiguity. Any help appreciated
The following is in my setup:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{name}",
defaults: null,
constraints: new { name = @"^[a-z]+$" }
);
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
app.UseWebApi(config);
}
}
The following are my two method calls
[Route("{UserId}/{Key}/{Source}", Name = "GetToken")]
[HttpGet]
public HttpResponseMessage GetToken(string UserId, string Key)
{
}
[Route("{Token}/{AcctNo}/{YearMonth}", Name = "GetInvoices")]
[HttpGet]
public HttpResponseMessage GetInvoices(string Token, string AcctNo, string YearMonth)
{
}
If I comment out one of these the other will get called but I can't seem to get it to work.
-------------Update------------...have modified the code as follows.
The "ApiByName" has been removed from the config startup. And below are the methods. I included a generic get that I hadn't shown before.
public IEnumerable<CustomListItem> Get()
{
// return new string[] { "value1", "value2" };
return _listItems;
}
[Route("GetToken/{UserId}/{Key}/{Source}")]
[HttpGet]
public HttpResponseMessage GetToken(string UserId, string Key, string Source)
{
...
}
[Route("GetInvoices/{Token}/{AcctNo}/{YearMonth}")]
[HttpGet]
public HttpResponseMessage GetInvoices(string Token, string AcctNo, string YearMonth)
{
...
}
Even with above changes calls always go to the generic Get (First method shown) Following are examples of calls from postman (as well as pasted directly in browser)??
http://localhost:37788/api/listitems/GetToken?UserId=Dshadle&Key=ABC&Source=Postman
http://localhost:37788/api/listitems/GetInvoices?Token=abc&AcctNo=123&YearMonth=2019/12