title: Handling multiple connection strings in asp.net core web api which came as a parameter
body: <p>In one my application I came across in a situation where user has to select the database needs to be used from the front-end.</p>
  
  <p>And then in every request the selected value is passing as a parameter as all are <code>web api</code> calls and the connection needs to be made based on the selected database.</p>
  
  <p>So currently I am writing the code for initializing the connection in every <code>action method</code>. like :-</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; GetData([FromQuery]string Id, [FromQuery]string database)
  {
      if(database=="A")
         connection conn=new connection("connStringA");//dummy code
      if(database=="B")
         connection conn=new connection("connStringB");//dummy code
      // and so on the logic......... 
  }
  </code></pre>
  
  <p>I can create a separate <code>method</code> also which will do same thing for me but again I do need have to call that <code>method</code> in each time for all the <code>actions</code>.</p>
  
  <p>Here also I have a <code>constructor</code> with <code>DI</code>.</p>
  
  <p>My question is is there any other better way of doing this without <code>writing/calling</code> in each <code>action</code>. I believe there should be but I am unable to get through it.</p>
  
  <p>What I am looking for like:-</p>
  
  <ul>
  <li>Any way to have this by injecting through <code>DI</code>.</li>
  <li>Any way to initialize by <code>constructor</code>.</li>
  <li>Any way by <code>action filters</code>.</li>
  <li>Or any better approach.</li>
  </ul>