根据条件在Web api路由映射中添加参数

时间:2018-02-19 07:28:30

标签: asp.net asp.net-web-api

在我的应用程序中有4个下拉列表(系列,类,系列,Fusebody)和一个搜索按钮。我必须根据这些下拉菜单的选择搜索产品表。可以使用该4个下拉列表中的任何一个下拉值或多个来完成此搜索。就像下面我试过的一样。

示例:来自一个下拉选择

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

示例:两个不同的下拉选择

[Route("api/KendoCascading/GetclRes/{cres}")]
    public HttpResponseMessage GetclRes(string cres)
    {
        using (CrossReferenceTool1Entities cp = new CrossReferenceTool1Entities())
        {
            var query = (from u in cp.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Class.Contains(cres)) select u).Distinct().ToList();
            HttpResponseMessage res;
            res = Request.CreateResponse(HttpStatusCode.OK, query);
            return res;
        }
    }

示例:三个下拉菜单选择

[Route("api/KendoCascading/GetresClsbyFam/{cres}/{family}")]
    public HttpResponseMessage GetresClsbyFam(string cres,int family)
    {
        var query = (from u in ep.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Class.Contains(cres) && u.ProductFMSFamiliesID==family) select u).ToList();
        HttpResponseMessage res;
        res = Request.CreateResponse(HttpStatusCode.OK, query);
        return res;
    }

像这样我到目前为止已经使用了api方法。但路线有很多种组合。如果可以根据以下条件添加参数:

[Route("api/KendoCascading/Getfaclapp/{family}/{cres}/{apRes}")]
    public HttpResponseMessage Getfaclapp(int family, string cres, string apRes)
    {
        using (CrossReferenceTool1Entities al = new CrossReferenceTool1Entities())
        {
            var query = (from u in al.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Class.Contains(cres) && u.ProductFMSFamiliesID==family && al.ProductApplications.Any(d=>d.ProductCode.Contains(u.ProductCode) && d.ProductApplicationName.Contains(apRes))) select u).ToList();
            HttpResponseMessage res;
            res = Request.CreateResponse(HttpStatusCode.OK, query);
            return res;
        }
    }
像那样。我已尝试但在web api路由中显示错误。它没有采用任何空值。怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

[Route("api/KendoCascading/GetResultByAll")]
public HttpResponseMessage GetResultByAll(int? family = null,string cres = null,string apps = null, int? seID = null)

并使用这样的网址:

  

API / KendoCascading / GetResultByAll家族= 1&安培; CRES = someCres

它应该做的伎俩