簡體   English   中英

將按鈕路由到C#中的靜態IP地址

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM