1

In my Application there are 4 dropdowns(family,class,series,Fusebody) and one search button. I have to search product table based on selections of these Dropdowns. This search can be done using any one dropdown value from that 4 dropdowns or multiple. Like below i have tried.

Example: from one dropdown selection

[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;
        }
    }

example: two different dropdown selection

[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;
    }

example: three dropdowns selection

[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;
        }
    }

Like this i have routed the api methods so far. But there are so many combinations to route. If its possible to add parameters based on conditions like below:

[Route("api/KendoCascading/GetResultByAll/{family}/{cres}/{apps}/{seID}")]
public HttpResponseMessage GetResultByAll(int family,string cres,string apps, int seID)
{
 if(family==0)
  {
   result=query1;
  }
    if(family==0 && cres==null)
{

    result=query2;
    }
        }

Like that. I have tried but in web api route its showing error. It is not taking any null values. How to do that?

learningMonk
  • 1,101
  • 13
  • 34

1 Answers1

0

Try this:

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

And use URL like this:

api/KendoCascading/GetResultByAll?family=1&cres=someCres

It should do the trick

garret
  • 1,134
  • 8
  • 16
  • Thank you very much garret. I have one more query. How to call that route map in angular service? Suppose parameters are (family=1, cres=null,seID=1). Can you please help me a little to do that? it will be very much helpful for me – learningMonk Feb 19 '18 at 09:36
  • just add family and seID to qury parameters and ignore nulls (they will by automaticly set to null) – garret Feb 19 '18 at 09:52
  • @user7411584 here You have answer with angular query params: https://stackoverflow.com/questions/13760070/angularjs-passing-data-to-http-get-request – garret Feb 19 '18 at 09:55
  • Thanks it is working fine. But if more of parameters is there for any method. Then many if conditions is required. Is there any process to use less number of if else conditions? if any better approach is there,please let me know. Thanks for your kind help. – learningMonk Feb 21 '18 at 11:05