0

this is my webapi controller very easy:

public class TreeController : ApiController
{
    [HttpGet]
    public List<TreeData> GetTreeData()
    { 
    }
}

this is my WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {

        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        //config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;

        // Configurazione filtro per global exception handling
       // config.Filters.Add(new WebApiUnhandledExceptionFilter());


        config.MapHttpAttributeRoutes();

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

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

this is my angular js controller function:

    $scope.getTreeData = function () {

        $http({
            method: "GET",
            url: '/api/Tree/GetTreeData',
            dataType: "json",
        }).then(function successCallback(response) {
            $scope.data = response.data;
        }, function errorCallback(response) {
            alert("trouble..");
        });
    };

    $scope.getTreeData();

why i get 404 ? i need to configure the web.xml ? what i need to do more ?

FrankTan
  • 1,626
  • 6
  • 28
  • 63
  • your not returning anything in your web api.. or are you doing this for simplicity? – Joshua Duxbury Apr 08 '16 at 21:01
  • for semplicity i have omitted the code of the method GetTreeData – FrankTan Apr 08 '16 at 21:03
  • 1
    Verify that you are calling `WebApiConfig.Register(GlobalConfiguration.Configuration);` to register your WebAPI routes. Also, make sure to call it *before* you call `RouteConfig.RegisterRoutes(RouteTable.Routes);`. – NightOwl888 Apr 08 '16 at 21:22
  • 1
    Did you try to enter the web api url from the browser? Is it working? localhost//api/Tree/GetTreeData – Dennis Nerush Apr 08 '16 at 21:23
  • Take a look at this http://stackoverflow.com/questions/34581571/angularjs-controller-actions-not-working/34581947#34581947 – Shyju Apr 08 '16 at 21:45
  • @NightOwl888 you answerd correctly :( thank you very much – FrankTan Apr 08 '16 at 22:18

0 Answers0