调用构造函数后收到的HttpResponseMessage参数

时间:2015-05-29 14:10:44

标签: c# asp.net constructor

我有一个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方法之前运行,因此数据库调用由于空连接字符串而失败。

如何在构造函数之前获取参数?

0 个答案:

没有答案