Read connection string from Web.config
This is an example of a custom request handler that retrieves the database connection string from Web.config.
This example is for ASP.NET and not for ASP.NET Core. ASP.NET Core projects use a appsettings.json file instead of Web.config
using System;
using System.Configuration;
using System.Data.SqlClient;
using Ventura;
using VenturaServer;
namespace AspNetServer
{
public class CustomRequestHandler : IVenturaRequestHandler
{
public void Authenticate(bool is_secure_connection, string username, string password, string databag)
{
if (is_secure_connection == false) // SSL is obligated
throw new Exception("Authentication is not allowed over an open (non-SSL) connection.");
if (username != "admin" || password != "password")
throw new Exception("Incorrect username and/or password.");
}
public AdoConnector LookupAdoConnector(string remote_connector_name)
{
// Read the connection string from Web.config
ConnectionStringSettings css = ConfigurationManager.ConnectionStrings["DefaultConnection"];
if (css == null)
throw new Exception("Connection string 'DefaultConnection' not found in Web.config");
return new AdoConnector(SqlClientFactory.Instance, css.ConnectionString);
}
}
}
An example of a Web.config with the DefaultConnection entry in it:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=(local);Initial Catalog=AdventureWorks2017;Integrated Security=SSPI;Max Pool Size=250;Connect Timeout=30;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>