Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API httpget with many parameters

I am trying to create my first REST service using WEB API to replace some of my postbacks in a web forms asp.net project. In the web forms project, when I browse to a new web page, I always get an ASP.net Application variable and a querystring value that helps me determine which database to connect to. In this old app, it connects to several different databases that all have the same schema and database objects but the data is different in each database

I am not sure the best way to pass these variables to a REST Service or if they should be part of the route or some other method.

So in a REST method like the one below

    // GET api/<controller>/5
    public string GetCategoryByID(int id)
    {
        return "value";
    }

I can get the category id and pass that to my database layer, but I also need the two variables mentioned above. I will need to obtain these variables in every call to my REST api in order to access the appropriate database. Should I use something like the following:

    // GET api/<controller>/5
    public string GetCategoryByID(int id, string applicationEnvironment, string organization)
    {
        return "value";
    }

Or should they be part of the route with something like this:

api/{appEnvironment}/{organization}/{controller}/{id}

This seems like a simple problem, but I am having trouble figuring out a solution.

like image 337
user2680562 Avatar asked May 13 '26 13:05

user2680562


1 Answers

I ended up passing extra parameters with my httpget call. I will probably follow this pattern unless I get some additional feedback.

    [HttpGet]
    public Company[] GetProgramCompanies(int id, [FromUri] string org, [FromUri] string appEnvir)
    {
        DataLayer dataAccess = new DataLayer(Utilities.GetConnectionString(org, appEnvir));
        IEnumerable<BudgetProgramCompanyListing> companies = dataAccess.GetProgramCompaniesListing(id).OrderBy(o => o.Company_Name);

        Company[] returnComps = new Company[companies.Count()];
        int count = 0;

        foreach (BudgetProgramCompanyListing bpc in companies)
        {
            returnComps[count] = new Company
            {
                id = bpc.Company_ID,
                name = bpc.Company_Name
            };
            count++;
        }

        return returnComps;
    }

Calling the above service with this url:

api/programcompanies/6?org=SDSRT&appEnvir=GGGQWRT

like image 118
user2680562 Avatar answered May 16 '26 04:05

user2680562