Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webapi控制器的设计_C#_Asp.net Web Api - Fatal编程技术网

C# webapi控制器的设计

C# webapi控制器的设计,c#,asp.net-web-api,C#,Asp.net Web Api,在现有的LOB应用程序中,我添加了新的WebAPI项目。由于应用程序中的现有层(域对象,DAL-ADO.NET-DataReader,BLL),我决定通过保留现有逻辑来使用Web API 第一个问题:这是正确的方法吗 BLL中有一个方法返回对象列表,并接收4个参数。所有这些输入参数都可以为NULL,在这种情况下,该方法返回对象的完整列表 第二个问题:如何为上述方法设计WEB api控制器 public static List<DomainObject> GetTata(int? pa

在现有的LOB应用程序中,我添加了新的WebAPI项目。由于应用程序中的现有层(域对象,DAL-ADO.NET-DataReader,BLL),我决定通过保留现有逻辑来使用Web API

第一个问题:这是正确的方法吗

BLL中有一个方法返回对象列表,并接收4个参数。所有这些输入参数都可以为NULL,在这种情况下,该方法返回对象的完整列表

第二个问题:如何为上述方法设计WEB api控制器

public static List<DomainObject> GetTata(int? param1,int? param2, int? param3, int? param4)
{
    List<DomainObject> return = new List<DomainObject>();

    using (Context context = new Context())
    {
        return = MyDAL.GetData(param1,param2,param3,param4, context);
    }

    return return ;
}
公共静态列表getta(int-param1、int-param2、int-param3、int-param4)
{
列表返回=新列表();
使用(上下文=新上下文())
{
return=MyDAL.GetData(param1、param2、param3、param4、context);
}
回归;
}
不知道数据意味着什么,但在设计WebApi时,你应该考虑“资源”(而不是“行动”)

我不喜欢方法是静态的(老实说,从来没有测试过它,我也不知道它是否有效),但是服务于响应的方法应该是实例方法(并且迟早你可能需要访问一些实例字段)

不知道这四个参数是什么,但您应该考虑访问此控制器的URL。让我们假设这个控制器返回类似“Customers”的内容。因此,我认为URL如下所示:

/api/Customers -> Get ALL customers
/api/Customers/{id} -> Get Customer of this id
额外的过滤器和子句(orderby,如果需要,分页)通常通过querystring传递(例如,看看OData是如何做到这一点的)。比如:

/api/Customers?name=foo -> Get ALL customers which its name starts with foo
/api/Customers?name=x&order=birthDate -> Get ALL customers which its name starts with x and ordered by birhDate.
因此,控制器应该将URL参数(路由和查询字符串)转换为DAL类所期望的参数。但是,将DAL类在大多数情况下期望的参数公开给WebApi并不是一个好主意

希望这有助于。。。要了解更多信息,您应该提供更多信息(参数的含义,等等)。;)