Asp.net核心2.0条件服务或条件连接字符串

时间:2017-08-26 07:34:06

标签: service asp.net-core http-headers startup

我正在使用Asp.net core 2.0开发web api。现在我需要的是使用不同的连接字符串,这些字符串将在用户的标题中指定。

我以前见过这个(不直接关于这个问题):

        app.Use(async (context, next) =>
        {
            if (string.IsNullOrWhiteSpace(context.Request.Headers["Authorization"]))
            {
                if (context.Request.QueryString.HasValue)
                {
                    var token = context.Request.QueryString.Value
                        .Split('&')
                    .SingleOrDefault(x => x.Contains("authorization"))?.Split('=')[1];

                    if (!string.IsNullOrWhiteSpace(token))
                    {
                        context.Request.Headers.Add("Authorization", new[] { $"Bearer {token}" });
                    }
                }
            }
            await next.Invoke();
        });

但是我该如何以这样的方式更改连接字符串呢?

我是朝着正确的方向前进的吗?

更新

我正在寻找这样的中间件:

        app.Use(async (context, next) =>
        {
            if (!string.IsNullOrWhiteSpace(context.Request.Headers["TestMode"]) && context.Request.Headers["TestMode"] == true)
            {
                // TODO Change the connection string here
            }
            await next.Invoke();
        });

但不知道TODO中要放什么。

0 个答案:

没有答案