.net核心-数据库URL解析器 [英] .net core - database url parser

查看:105
本文介绍了.net核心-数据库URL解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Heroku上部署.net核心应用程序。 Heroku提供以下格式的数据库URL:

I am deploying my .net core app on Heroku. Heroku provides the database url in the following format:

[database type]://[username]:[password]@[host]:[port]/[database name]

EF期望连接字符串采用以下格式:

EF expects the connection string to be in the format:

host=[host];user id=[username];password=[password];database=[database name];pooling=true;

是否可以使用任何解析器进行转换,或者我必须为自己编写逻辑。 / p>

Is there any parser available to convert, or I have to write the logic for the same myself.

推荐答案

简单的网址解析有效:

Uri url;
bool isUrl = Uri.TryCreate("postgres://1user:1password@dbserver.com:4568/testdb", UriKind.Absolute, out url);
if(isUrl) {
    Console.WriteLine("Host: "+url.Host);
    Console.WriteLine("Port: "+ url.Port);
    Console.WriteLine("Database: "+ url.LocalPath.Substring(1));
    Console.WriteLine("Username: "+ url.UserInfo.Split(':')[0]);
    Console.WriteLine("Password: "+ url.UserInfo.Split(':')[1]);
    var connectionUrl = $"host={url.Host};username={url.UserInfo.Split(':')[0]};password={url.UserInfo.Split(':')[1]};database={url.LocalPath.Substring(1)};pooling=true;";
    Console.WriteLine(connectionUrl);
}

这篇关于.net核心-数据库URL解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆