title: How can I dynamically set connection string in MVC 5 based on incoming request?
body: <p>I have an MVC 5 application that is currently single tenant only, but that is becoming unmanageable as our client-base grows. </p>
  
  <p>I would like to turn this application into a multi-tenant app, accessing a distinct database for each client.</p>
  
  <p>The client's are easily distinguishable by their domain, which I can access through Request.Url.Host or other methods, but once I have this property, I am having trouble setting it in the Business logic project within the context of that request only. </p>
  
  <p>The Business logic project is referenced by the main Web project, but that project is blink to the Request object, so I do not know how to get that session information dynamically each time the baseDataAccess (which contains the connection strings) object is instantiated.</p>
  
  <p>I've spent many many hours on this, and have tried using Ninject to resolve the dependency with no success. I just can't seem to figure out how to get the dynamic Request object or any of its properties to assign transiently to the baseDataAccess object. </p>
  
  <p>The last thing I tried was using and IActionFilter using the OnActionExecuting method, but I am still unable to figure out how to set the value of the URL for each request dynamically in a project that has no context for the current request. </p>
  
  <p>This is the class I want to set dynamically. the _connectionString is the dynamic property. I have methods to build the connection string based on the url, so if I can set the _domain property dynamically and transiently I'll be able to get the connection string.</p>
  
  <pre><code>public class baseDataAccess : IDataAccess
  {
      private static IClientConnection _clientConnection;
      private static string _connectionString;
      public static string _domain;
  </code></pre>
  
  <p>...</p>
  
  <p>This is the last attempt I made, adding the OnActionExecuting method to each controller. How can I set these properties dynamically and transiently?</p>
  
  <pre><code>    public void OnActionExecuting(ActionExecutedContext filterContext)
      {
          IClientConnection client = new ClientConnectionFactory(Request.Url.Host);
          IDataAccess dataAccess = new baseDataAccess(client);
  
      }
  </code></pre>