简体   繁体   English

在ASP.NET Web API中添加额外的get方法

[英]Add an extra get method in asp.net web api

I'm a very new comer to the asp.net web api world. 我是asp.net Web API世界的新手。 I've got the basic understanding of get(), put(), post() and delete. 我对get(),put(),post()和delete有基本的了解。

In my application, I require two more get() method. 在我的应用程序中,我还需要两个get()方法。 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. angularjs控制器中已经有一个$http.get(..)

My question is, how can I call the two additional get() methods from angular controller. 我的问题是,如何从角度控制器中调用另外两个get()方法。

Well, I haven't used asp.net mvc in forever. 好吧,我还没有永远使用过asp.net mvc。 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: 您还可以像下面这样在routeconfig中指定路由:

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. 并非您具有指定的路由,而是可以为每个路由添加有角$ 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()

What you want to do is write costum angular resource method, to call your API. 您要做的是编写Costum Angle资源方法,以调用您的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). 使用有角度的$ resource而不是$ http->是更常见的用法(并且更面向REST:$ resource包装$ http以便在RESTful Web API场景中使用)。

  2. Read about it 阅读有关

  3. Find how to add a resource to the $resource service. 查找如何将资源添加到$ resource服务。

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

    }) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM