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

Skpaul

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

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

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

鲈鱼

好吧,我还没有永远使用过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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章