Web Api返回状态500

时间:2015-10-14 20:00:08

标签: asp.net-mvc asp.net-web-api asp.net-web-api2

编写一个小型MVC应用程序,该应用程序使用我在Azure上创建和托管的Web API。当我尝试调用该方法时。它返回状态500.我已经尝试过小提琴手,看看有什么错误被退回,但我没有得到任何回报

我的客户代码

    public async Task<Planets> GetPlanets()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://newplanet.azurewebsites.net/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // New code:
            HttpResponseMessage response = await client.GetAsync("api/planet");
            if (response.IsSuccessStatusCode)
            {
                var res = await response.Content.ReadAsStringAsync();
                var listOfPlanets = JsonConvert.DeserializeObject<Planets>(res);

                return listOfPlanets;
            }
        }

        return null;
    }

我的服务类,

    public class PlanetController : ApiController
{
    public IPlanetService _planetService;

    public PlanetController(IPlanetService planetService)
    {
        _planetService = planetService;
    }

    // GET: api/values
    [HttpGet]
    public Planets Get()
    {
        var result = _planetService.GetPlanets();

        return result;
    }
}

我认为问题在于我如何设置web api config

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
        // Web API routes
        config.MapHttpAttributeRoutes();

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

0 个答案:

没有答案