0

The web api Get methods are giving me 404 error . Here is what i am doing

Web Api routing

 config.Routes.MapHttpRoute(
        name: "DefaultApi2",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
        );

Web Api Controller methods

    [HttpGet]
    public GameDto GetGameById(int gameId)
    {
      //My code here
    }
    [HttpGet]
    public List<GameDto> GetUserGames(int userId)
    {
      //My code here
     }

    [HttpGet]
    [ActionName("GetPublicGames")]
    public List<GameDto> GetPublicGames(int requestUserId, int start, int limit)
    {
      //My code here
    }
     [HttpPost]
    [ActionName("CreateGame")]
    public HttpResponseMessage CreateGame(GameDto gameDto)
    {
     //My code here
    }
    [HttpPost]
    [ActionName("UserRequestToJoin")]
    public HttpResponseMessage ProcessGameInvitationRequest(GameInvitationDto gameInvitationDto)
    {
      //My code here
    }

Client code

try
        {             
             HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:59580/");            
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            var response = await client.GetAsync("api/Game/GetGameById?gameId=1");
            response.EnsureSuccessStatusCode(); // Throw on error code.                
            var gameDto = await response.Content.ReadAsAsync<GameDto>();
            MessageBox.Show(gameDto.Name);
        }
        catch (Newtonsoft.Json.JsonException jEx)
        {
            // This exception indicates a problem deserializing the request body.
            MessageBox.Show(jEx.Message);
        }
        catch (HttpRequestException ex)
        {
            MessageBox.Show(ex.Message);
        }

When i run this code from localhost all web api methods execute with out any issue .But in production only post methods are working and Get methods are returning 404 error .
Production is : .Net 4.5 and IIS 7

RK Reddy
  • 81
  • 1
  • 8
  • what are the request urls for which you are seeing this issue? also i see that the route is named 'DefaultApi2', do you have any other webapi routes also configured?... if yes,could you share them? – Kiran Jan 08 '14 at 15:51
  • I am seeing the issue for all the [HttpGet] methods. 'DefaultApi2' -this is only the route configured . – RK Reddy Jan 08 '14 at 16:14
  • Are you sure that all the methods are working in your localhost..based on your actions and route, i suspect if that's correct...you can check my reply below to fix this.. – Kiran Jan 08 '14 at 16:21
  • When i run the same in production i am getting the is error No HTTP resource was found that matches the request URI 'http://xx.xxx.xx.com/api/Game/GetGameById?gameId=11'. – RK Reddy Jan 08 '14 at 23:17
  • In production i am getting the correct results when i use the URL like this `http:\\xx.xxx.xx.com/api/Game/GetGameById/?gameId=11'; All the Post and Get methods working correctly now .The Issue is fixed for now but i am not sure the basics behind it. – RK Reddy Jan 09 '14 at 14:42
  • Does [this link](http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-web-api-controller) help ? – AechoLiu Mar 11 '14 at 05:59
  • Have answered a similar question here: https://stackoverflow.com/questions/14325794/web-api-multiple-post-methods/22488243#22488243 – Vaibhav Mar 18 '14 at 18:46

0 Answers0