1

我是asp.net web api 世界的新手。我对 get()、put()、post() 和 delete 有了基本的了解。

在我的应用程序中,我需要另外两个 get() 方法。下面给出解释——

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.
    }
}

angularjs 控制器中已经有一个$http.get(..)

我的问题是,如何get()从角度控制器调用这两种附加方法。

4

2 回答 2

2

好吧,我从来没有使用过asp.net mvc。但是您可以执行以下操作:

 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.
    }
}

您还可以像这样在 routeconfig 中指定路由:

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

你必须为自己努力。您可以在此处此处阅读更多相关信息。

并不是说您有指定的路线,您可以为每条路线添加角度 $http.gets 。

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()
于 2014-10-20T06:49:23.930 回答
0

您要做的是编写 costum 角度资源方法来调用您的 API。

  1. 使用角度 $resource 而不是 $http - > 这是更常见的用法(并且更面向 REST:$resource 包装 $http 以用于 RESTful Web API 场景)。

  2. 阅读它

  3. 了解如何将资源添加到 $resource 服务。

    这是一个例子:

    .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'
        }
    });
    

    })

于 2014-10-20T06:40:30.427 回答