1

I'm a very new comer to the asp.net web api world. I've got the basic understanding of get(), put(), post() and delete.

In my application, I require two more get() method. An explanation is given below-

public class StudentController : ApiController 
{
    public IEnumerable Get()
    {
        //returns all students.
    }

    //I would like to add this method=======================
    [HttpGet]
    public IEnumerable GetClassSpecificStudents(string classId)
    {
        //want to return all students from an specific class.
    }

    //I also would like to add this method=======================
    [HttpGet]
    public IEnumerable GetSectionSpecificStudents(string sectionId)
    {
        //want to return all students from an specific section.
    }

    public Student Get(string id) 
    {
         //returns specific student.
    }
}

There is already a $http.get(..) in angularjs controller.

My question is, how can I call the two additional get() methods from angular controller.

2
  • will all the urls be the same? you might wanna update the url to contain filters like /students/:filter/:filterId such as students/section/sectionId Commented Oct 20, 2014 at 6:29
  • @cbass - I don't have any idea. Commented Oct 20, 2014 at 6:31

2 Answers 2

2

Well, I haven't used asp.net mvc in forever. But you be able to do something like:

 public class StudentController : ApiController 
 {
    [Route("students")]
    public IEnumerable Get()
    {
    //returns all students.
    }

    //I would like to add this method=======================
    [HttpGet]
    [Route("students/class/{classId}")]
    public IEnumerable GetClassSpecificStudents(string classId)
    {
        //want to return all students from an specific class.
    }

    //I also would like to add this method=======================
    [HttpGet]
    [Route("students/section/{sectionId}")]
    public IEnumerable GetSectionSpecificStudents(string sectionId)
    {
        //want to return all students from an specific section.
    }
    [Route("students/{id}")]
    public Student Get(string id) 
    {
         //returns specific student.
    }
}

You could also specify routes in the routeconfig like this:

routes.MapRoute(
    name: "students",
    url: "students/class/{classId}",
    defaults: new { controller = "Student", action = "GetClassSpecificStudents", id = UrlParameter.Optional }
);

You have to try for your self. And you can read more about it here and here.

Not that you have your specified routes you can add angular $http.gets for each route.

var url = "whateverdoma.in/students/"
$http.get(url)
   .success()
   .error()

var url = "whateverdoma.in/students/class/" + classId;
$http.get(url)
   .success()
   .error()

var url = "whateverdoma.in/students/filter/" + filterId;
$http.get(url)
   .success()
   .error()
Sign up to request clarification or add additional context in comments.

1 Comment

You have explained well. The scenario is clear to me. Thanks a lot. +1.
0

What you want to do is write costum angular resource method, to call your API.

  1. Use angular $resource and not $http - > it is the more common usage (and more REST oriented: $resource wraps $http for use in RESTful web API scenarios).

  2. Read about it

  3. Find how to add a resource to the $resource service.

    Here is an example:

    .factory('Store', function ($resource, hostUrl) {
    var url = hostUrl + '/api/v3/store/';
    
    return $resource("", {  storeId: '@storeId' }, {            
        getSpecific: {
            method: 'GET',
            url: hostUrl + '/api/v3/store-specific/:storeId'
        }
    });
    

    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.