我有一个API控制器,当从客户端调用时传递数据库凭据。
public class ForemanController : ApiController
{
static SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder ();
public ForemanController()
{
//database calls with connection string
}
public void Get(string DataSource, string InitialCatalog, string UserID, string Password)
{
connectionString.DataSource = DataSource;
connectionString.InitialCatalog = InitialCatalog;
connectionString.UserID = UserID;
connectionString.Password = Password;
}
}
get方法工作正常,我可以看到通过客户端代码传递的凭据:
static HttpClient client = new HttpClient();
HttpResponseMessage resp = client.GetAsync("api/foreman?DataSource=dxdv&InitialCatalog=Q19410&UserID=tunld&Password=did").Result;
我的问题是ApiController中的构造函数在get方法之前运行,因此数据库调用由于空连接字符串而失败。
如何在构造函数之前获取参数?