[英]Route a button to a static IP address in C#
在.NET领域,我仍然是个婴儿,我被赋予了我似乎无法解决的任务。
我需要一个应用程序来连接到多个数据库。 数据库具有静态IP地址。
因此,基本上,当用户单击按钮database1时,它将进入IP1,如果他们单击database2,它将自动路由到IP2。
我觉得自己已经忽略了一些可能如此简单的事情。
谢谢
1)在web.config文件中有2个连接。 2)在一个按钮上,单击事件指的是第一个IP,在另一个按钮上单击,请连接另一个IP。
以前从未做过,但是有一些想法可以实现。 在我的项目中,我引用了2个数据库。 PostgreSQL和Oracle。 在我的应用程序中,我有几个提供程序类,它们实现数据库访问并为此使用Executor类。 要创建此执行程序的实例,我需要使用数据库的IP(在本地网络中)传递连接字符串。 我从Web.config文件中获取的连接字符串
//Here is the connection strings with IP, Login, and DBname from web.config You need to configure it dynamicaly
private string PostgreSQLConnection = WebConfigurationManager.ConnectionStrings["PostgreSQLConnection"].ConnectionString;
private string OracleConnection = WebConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
using (PostgreSQLExecutor executor = new PostgreSQLExecutor(PostgreSQLConnection))
{
executor.ExecuteQuery(@"query");
}
using (OracleSQLExecutor executor = new OracleSQLExecutor(OracleConnection))
{
executor.ExecuteQuery(@"query");
}
传递任何字符串作为连接字符串没有问题。 我不知道您的应用程序使用什么数据库访问库,但是我确定它具有类wow,可以像我的示例中那样配置会话。 因此,我认为您只需要找到数据库配置类并实现动态配置逻辑即可。 最好是像这样用数据库部分创建路由。
routes.MapRoute(
name: "Default",
url: "{databaseSection}/{controller}/{action}/{id}",
defaults: new { databaseSection = "sectionName1", controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { databaseSection = "sectionName1|sectionName2" }
);
因此,如果应用程序将URL切换到site.com/sectionName2/home/index,则可以检索节名称并将数据库访问权限配置为全局行为。 例如在global.asax Application_BeginRequest事件中
protected void Application_BeginRequest(object sender, EventArgs e)
{
string section = Request.RequestContext.RouteData.Values["databaseSection"].ToString();
if(section == sectionName2)
{
do some stuff to configure your database session
}
else
{
default behavior
}
}
或者,您可以为此使用cookie。 希望能帮助到你。 对不起,我的英语/:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.