0

Request URL:

/api/Test/Retrieve/14012638/?14012647

Endpoint:

[Route("Retrieve/{firstid}/{secondid?}")]       
public async Task<TestAPI> Retrieve(long firstid, long secondid)
  • Use a sniffer like wireshark or fiddler and check the status of the response. A good response should be 200 OK. You must be getting something else and underlining code is not giving the exact error number. The error number will help determine how to fix issue. – jdweng May 08 '20 at 18:03
  • I guess you have api/test declared. Can [this](https://stackoverflow.com/questions/38249019/web-api-optional-parameters) help? – Štefan Bartoš May 08 '20 at 18:03
  • its not even hitting api – TestforAngular May 08 '20 at 18:03
  • See https://stackoverflow.com/questions/24769379/why-is-it-that-no-http-resource-was-found-that-matches-the-request-uri-here – jdweng May 08 '20 at 18:04
  • Can you provide more code, please? Especially class attributes. Thanks – Štefan Bartoš May 08 '20 at 20:10

1 Answers1

0

It is always better to place your route at the controller level and your HTTP verb at the action level.

For your requirement, you can do this like below. I assume you are doing a GET operation.

[HttpGet("Retrieve/{firstid}/{secondid?}")]    
public async Task<WellsTradeAccountsInformation> Retrieve(long firstid, long secondid) { … }

If your controller' route is api/Test, then you can call this as:

GET /api/Test/Retrieve/14012638/14012647

or

GET /api/Test/Retrieve/14012638

As the second parameter is an optional value type, if you don't pass it will default to 0.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
ghosh-arun
  • 51
  • 4
  • in some cases we dont need to pass second parameter and is optional.is there any way we can make this work for request type:/api/Test/Retrieve/14012638/?14012647 – TestforAngular May 08 '20 at 23:00
  • [HttpGet("Retrieve/{firstid}/{secondid?}")] public async Task Retrieve(long firstid, long secondid) – TestforAngular May 08 '20 at 23:12
  • @TestforAngular I could able to get the response properly with route which I mentioned in the answer, could you please share the route you are trying. – ghosh-arun May 09 '20 at 04:53
  • @TestforAngular as the second parameter mentioned as optional with '?' [HttpGet("Retrieve/{firstid}/{secondid?}")] you can ignore the second parameter if you don't want to pass in route like this GET /api/Test/Retrieve/14012638'. I tried with fiddler and it is giving successful response for both api routes – ghosh-arun May 09 '20 at 05:04
  • GET /api/Test/Retrieve/14012638/?14012647 should work with [Route("Retrieve/{firstid}/{secondid?}")] public async Task Retrieve(long firstid, long secondid = 0) Issue is secondid always set to zero even after passing value as second parameter.How can i rewrite the code to accept second value from API – TestforAngular May 11 '20 at 18:18
  • I see you passing '?' in second parameter could you please remove and try. – ghosh-arun May 11 '20 at 18:22
  • yes that works but is there any way we can change in code rather than url? – TestforAngular May 11 '20 at 18:32
  • This is my code [Route("Retrieve/{firstid}/{secondid?}")] public async Task Retrieve(long firstid, long secondid = 0) – TestforAngular May 11 '20 at 18:33
  • Actually when u call GET /api/Test/Retrieve/14012638/?14012647 it will not consider the second parameter as long type since you are passing ? in it. Why you are passing ? in the parameter any specific requirement? Could you please eloborate if any. – ghosh-arun May 11 '20 at 18:40
  • its the ui team who want to pass paramter like this so can anything can be done on controller side – TestforAngular May 11 '20 at 19:13
  • If u want the parameter to accept with ? Mark change the second parameter to string instead of long. – ghosh-arun May 11 '20 at 19:24
  • If u want the parameter to accept with ? Mark change the second parameter to string instead of long. I have very limited knowledge on your requirement but the best practice would be to send second parameter only if required as it is optional, and if passing the second parameter then pass it in proper format of the type your api expecting. – ghosh-arun May 11 '20 at 19:33