0

I'm learning and creating sample WebApi. In Web Api project, controller name is Producer. I am trying to get the firstname using postman but not getting any result.

I have followed this Multiple optional parameters web api attribute routing but not getting result.

From postman I am passing request as follows.

 http://localhost:62116/api/Producer/GetUserAgencyDetails/?firstname=peter

Web Api header is as below.

[HttpGet]            
[Route("GetUserAgencyDetails/{firstName}/{lastName}/{agencyName}/{emailAddress}/{userId}")]
public async Task<User> GetUserAgencyDetails(string firstName = "", string lastName = "", string agencyName = "", string emailAddress = "", string userId ="")
{    
  //some code
}

Please let me know what mistake I am doing.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Roger
  • 11
  • 4
  • Did you debug the web API code to see why it is not returning any result? Are you getting 200 response code when you call the web API from postman? What is the url of the API you are trying to make request to? – Chetan Nov 16 '19 at 12:59
  • Show us the Postman request you're trying to send. – Divyang Desai Nov 16 '19 at 12:59
  • 1
    I have doubt in your routing that you have mentioned for your web API method. Just remove all the fields and forward slash after GetUserAgencyDetails and then hit the API again.. I am sure it will work – vijay sahu Nov 16 '19 at 13:09
  • You were trying with querystring but you defining route by parameter. If you prefer parameters then avoid queystring. – Md. Abdul Alim Nov 16 '19 at 14:22

1 Answers1

0

I think your routing is incorrect, try using it like this:

[HttpGet]            
[Route("Api/Producer/GetUserAgencyDetails")]
public async Task<User> GetUserAgencyDetails(string firstName = "", string lastName = "", string agencyName = "", string emailAddress = "", string userId ="")
{    
  //some code
}

that could've make it work, but give in case it did not, give a check at the route configuration, it should look something like this:

              config.Routes.MapHttpRoute(  
                name: "DefaultApi",  
                routeTemplate: "api/{controller}/{action}/{id}",  
                defaults: new  
                {  
                    id = RouteParameter.Optional  
                }  
            );  
MorenajeRD
  • 849
  • 1
  • 11
  • 27