【问题标题】:Add an extra get method in asp.net web api在 asp.net web api 中添加一个额外的 get 方法
【发布时间】:2014-12-03 05:07:16
【问题描述】:

我是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() 方法。

【问题讨论】:

  • 所有的网址都一样吗?您可能想要更新 url 以包含过滤器,例如 /students/:filter/:filterId,例如 students/section/sectionId
  • @cbass - 我不知道。

标签: javascript asp.net-mvc angularjs asp.net-web-api angular-http


【解决方案1】:

好吧,我从来没有使用过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 }
);

你必须为自己努力。你可以阅读更多关于它的信息herehere

并不是说您有指定的路线,您可以为每条路线添加角度 $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()

【讨论】:

  • 你解释得很好。这个场景对我来说很清楚。非常感谢。 +1。
【解决方案2】:

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

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

  2. Read about it

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

    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2012-09-28
    • 2019-09-18
    • 2015-10-09
    • 2019-07-18
    相关资源
    最近更新 更多