Answered by:
Get with string parameter not getting invoked

Question
-
User1126057398 posted
Filtering based on a string parameter called emailId. I am not sure for what reason the get method with string parameter doesnot get invoked.
public async Task<Users> CheckIfEmailExists(string emailId)
{
HttpClient client = ClientFactory.GetClient();
Users usr = null;
var result = string.Empty;
HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}", emailId));
if (response.IsSuccessStatusCode)
{
response.EnsureSuccessStatusCode();
HttpContent content = response.Content;
string jsonContent = await content.ReadAsStringAsync();
usr = JsonConvert.DeserializeObject<Users>(jsonContent);
}
return usr;
}In Controller, defined it like below:
[RoutePrefix("UsersData")]
public class UsersController : ApiController
{[Route("CheckEmailExists/{emailId}")]
[HttpGet()]
public Users CheckIfEmailExists(string emailId)
{
UsersService us = new UsersService();
Users user = us.CheckIfEmailExists(emailId);
return user;
}}
Monday, March 5, 2018 9:01 AM
Answers
-
User1126057398 posted
Thanks for replying. Got the solution. Only issue was that I forgot to apply '/' at the end in HttpResponse (ie HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}/", emailId));):
public async Task<Users> CheckIfEmailExists(string emailId)
{
HttpClient client = ClientFactory.GetClient();
Users usr = null;
var result = string.Empty;
HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}/", emailId));
if (response.IsSuccessStatusCode)
{
response.EnsureSuccessStatusCode();
HttpContent content = response.Content;
string jsonContent = await content.ReadAsStringAsync();
usr = JsonConvert.DeserializeObject<Users>(jsonContent);
}
return usr;
}In Controller, defined it like below:
[RoutePrefix("UsersData")]
public class UsersController : ApiController
{[Route("CheckEmailExists/{emailId}")]
[HttpGet]
public Users CheckIfEmailExists(string emailId)
{
UsersService us = new UsersService();
Users user = us.CheckIfEmailExists(emailId);
return user;
}}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 5, 2018 12:25 PM
All replies
-
User1100692814 posted
Hi,
Can you try adding [FromUri] to the CheckIdEmailExists <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="8" data-gr-id="8">action.</g>
[Route("CheckEmailExists/{emailId}")] [HttpGet()] public Users CheckIfEmailExists([FromUri] string emailId) { UsersService service = new UsersService(); Users user = service.CheckIfEmailExists(emailId); return user; }
Also, don't you require an additional / i.e.
HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}/", emailId))
Hope this helps.
D
Monday, March 5, 2018 10:34 AM -
User1126057398 posted
Thanks for replying. Got the solution. Only issue was that I forgot to apply '/' at the end in HttpResponse (ie HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}/", emailId));):
public async Task<Users> CheckIfEmailExists(string emailId)
{
HttpClient client = ClientFactory.GetClient();
Users usr = null;
var result = string.Empty;
HttpResponseMessage response = await client.GetAsync(string.Format("UsersData/CheckEmailExists/{0}/", emailId));
if (response.IsSuccessStatusCode)
{
response.EnsureSuccessStatusCode();
HttpContent content = response.Content;
string jsonContent = await content.ReadAsStringAsync();
usr = JsonConvert.DeserializeObject<Users>(jsonContent);
}
return usr;
}In Controller, defined it like below:
[RoutePrefix("UsersData")]
public class UsersController : ApiController
{[Route("CheckEmailExists/{emailId}")]
[HttpGet]
public Users CheckIfEmailExists(string emailId)
{
UsersService us = new UsersService();
Users user = us.CheckIfEmailExists(emailId);
return user;
}}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 5, 2018 12:25 PM