3

I have two HttpPost methods in my api controller and when i am hitting my api i am getting 500 internal server error.

But surprisingly when i remove one of the HttpPost methods i am able to successfully hit my api and retrieve the info i want.

Any idea what might be the problem?

these are the two methods

 [HttpPost] [ActionName("PostUser")] public HttpResponseMessage PostUser([FromBody]string id) { var accessToken = id; var client = new FacebookClient(accessToken); dynamic result = client.Get("me", new { fields = "name,email" }); string name = result.name; string email = result.email; var existingUser = this.Repository.FindByUserIdentity(name); if (existingUser == null) { var newUser = new User { Username = name, Email = email, }; var success = this.Repository.CreateAccount(newUser); if (!success) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } //return created status code as we created the user return Request.CreateResponse<User>(HttpStatusCode.Created, newUser); } return Request.CreateResponse(HttpStatusCode.OK); } [HttpPost] [ActionName("PostEmail")] public HttpResponseMessage PostEmail([FromBody]string id) { var accessToken = id; var client = new FacebookClient(accessToken); dynamic result = client.Get("me", new { fields = "email" }); string name = result.name; string email = result.email; var existingUser = this.Repository.FindByUserIdentity(name); if (existingUser == null) { var newUser = new User { Email = email }; var success = this.Repository.CreateAccount(newUser); if (!success) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } //return created status code as we created the user return Request.CreateResponse<User>(HttpStatusCode.Created, newUser); } return Request.CreateResponse(HttpStatusCode.OK); } 

EDIT:

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Competition", action = "all", id = UrlParameter.Optional } ); } 
2
  • Could you pls post route config? Commented Oct 24, 2012 at 10:03
  • @CuongLe posted the route config Commented Oct 24, 2012 at 10:36

1 Answer 1

4

It is becuase you have two Post methods in your controller and by covention WebApi only allow one Post method.

To configure two or multiple Post methods you have to configure your route settings.

For detail have a look at the following answer

Multiple HttpPost method in MVC4 Web API Controller

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton!! :D I have been banging my head about it all day!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.