-2

I have CourseAPi Controller that containing follwing method :

 public IEnumerable<CoursesDTO> Get(int id)
    {
        var x = _uniManager.GetCourses(id);
        return x;
    }

i want to send id from URl to this method using Angualr js the AngularJs controller :

app.controller('CRUD_OperController', function ($scope, $filter, CRUD_OperService, $location) {



GetAllRecords1();
function GetAllRecords1() {
    var id = 12;
    var promiseGetSingle = CRUD_OperService.get(id);
    promiseGetSingle.then(function (pl)
    { $scope.Courses = pl.data },
          function (errorPl) {
             // $log.error('Some Error in Getting Records.', errorPl);
          });
}

});

My angular service :

app.service('CRUD_OperService', function ($http) {



//Get All Student  
this.getAllStudent = function () {

    return $http.get("/api/CourseApi/" );
}

//Get Single Records

this.get = function (id) {
    return $http.get("/api/CourseApi/" + id);
}

});

my webapi config

 public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();



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

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

        );

        config.EnableSystemDiagnosticsTracing();
    }

Update : it return me error GET http://localhost:54070/api/CourseApi/12 404 (Not Found)

salah
  • 41
  • 2
  • 11

2 Answers2

0

This is the format I have used for such a task:

   this.$http({
        method: 'GET',
        url: 'api/courseapi/' + id
    }).then(function (pl) { 
        $scope.Courses = pl.data
    }, function (errorPl) {
          $log.error('Some Error in Getting Records.', errorPl);
    });

Is this what you are asking?

Rob10e
  • 319
  • 2
  • 9
  • not working for me return this error : ReferenceError: id is not defined – salah Aug 20 '15 at 12:44
  • What's the error? There are many other things that could cause an error. What's the name of your controller class? How is the routing set up? – Rob10e Aug 20 '15 at 12:50
  • check edit for web api confic and my api controller name (CourseApi) – salah Aug 20 '15 at 13:00
  • The name of your controller class needs to end in Controller and be in the Controllers folder. `Controllers\CourseApiController.cs` with class name: `public class CourseApiController`. Also, I am updating the url in the example I gave to reflect your class name. – Rob10e Aug 20 '15 at 13:15
  • 1) Did you rename / relocate your webapi controller? 2) Does record with Id #12 exist? – Rob10e Aug 20 '15 at 13:35
  • my web api controller under controller folder and id=12 is for test becuase give my error undediend id – salah Aug 20 '15 at 14:28
0

I can suggest two options:

The first one: configure routes in your angular controller using ng-route, inject $routeParams module and then retrieve the parameters using it. See this link and the Angular doc for more info.

The second option is to get url location and manipulate string in order to get the parameter:

var tokens = $location.absUrl().split("/");
var id = tokens[5]

I prefer the first option, it's "cleaner" than the second. Hope it can help you!

Community
  • 1
  • 1
Hernan Guzman
  • 1,235
  • 8
  • 14