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.

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? Commented Nov 16, 2019 at 12:59
  • Show us the Postman request you're trying to send. Commented Nov 16, 2019 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 Commented Nov 16, 2019 at 13:09
  • You were trying with querystring but you defining route by parameter. If you prefer parameters then avoid queystring. Commented Nov 16, 2019 at 14:22

1 Answer 1

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  
                }  
            );  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.