web api Get方法给我404错误。这就是我正在做的事情
网络Api路由
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Web Api控制器方法
[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
}
客户代码
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);
}
当我从localhost运行此代码时,所有web api方法都会执行任何问题。但是在生产中只有post方法正在运行而Get方法返回404错误。
生产是:.Net 4.5和IIS 7