Search code examples
c#.netdatabaseasp.net-web-api

Change database based on request origin


I am developing a web application which will be run by multiple companies. Each company will have its own instance of that web application. And each company has its own database (the web applications and databases will be running on the same server).

Each of these instances should be talking to one single Web API application to retrieve data from their database for that specific company.

Now I am wondering whether it is a good practice to change the database connection string in the Web API application based on the request origin? Or am I overlooking something?


Solution

  • There are things that need to be clarified:

    1. How do you know which database to use based on the request?
    2. What kind of technology do you use for db communication?
    3. What do you mean by saying that you want to change Web API connection string based on the request origin? And where?

    Anyways, the application is pretty funny. You need to have a connection string factory that returns connection string based on your request. You will need to decide where to put your connection string. Database or maybe some config settings?

    Alternatively you can consider hosting your WebAPI with ASP.NET web sites and have different connection string for each tenant. This would also work unless you don't want to share database connection string with people who have access to websites.