如何在web api中使用路径参数和查询参数。 [英] How to use path param and query param in web api.

查看:591
本文介绍了如何在web api中使用路径参数和查询参数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用restfull网络服务开发web api。

如何在网络中使用路径参数和查询parm api's。

项目类型是asp.net mvc 4

任务是使用单一方法完成多项任务。

例如公共学生( string id)

{

var student = listOfStudents.find(st => st.Id = id);

返回学生;

}

Hi,

I am developing web api's using restfull web services.
How to use path param and query parm in the web api's.
Project type is asp.net mvc 4
The task is to use single method to do multiple task.
e.g public Student(string id)
{
var student=listOfStudents.find(st=>st.Id=id);
return student;
}

推荐答案

您不应该。使用单一方法执行具有有意义名称的单个任务。



如果你绝对必须,添加描述动作的参数即公共学生(字符串id,ActionEnum动作){/ *无论* /} ActionEnum将以一些有意义的方式列出可用的动作,即ActionEnum.Insert,ActionEnum.GetStudent,ActionEnum.GetStudents等...



然而,无论你把它放到哪个枚举应该实际上是单独的方法。

注意单一方法不会简化,但会使服务复杂化。



是的,你只有一种方法,但是调用者必须知道哪些参数必须采取行动。并且该方法将变得很大,包含switch语句和一些代码重复。



而是有一个方法

GetStudents {return listOfStudents)

GetStudent(字符串id){return listOfStudents.find(st:> st.Id = id)

InsertStudent(Student s){}

等......
You shouldn't. Use single method to do single task with meaningful names.

If you absolutely have to, add parameter that describes the action i.e. public Student(string id, ActionEnum action) { /*whatever*/} where ActionEnum will list available actions in some meaningful way i.e. ActionEnum.Insert, ActionEnum.GetStudent, ActionEnum.GetStudents etc...

However, whatever you put into that enum should actually be separate method.
Note that single method will not simplify, but complicate the service.

True, you will have only one method, but the callers will have to know which parameters have to go for action. And also the method will get large with switch statements and maybe some code duplication.

Instead have a methods
GetStudents {return listOfStudents)
GetStudent (string id) { return listOfStudents.find(st :> st.Id = id)
InsertStudent (Student s) {}
etc...


这篇关于如何在web api中使用路径参数和查询参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆