This is a really broad question, and is highly dependent on your application. E.g., where are you storing your connections string? When and where are you first accessing it? I can provide a broad overview of how to approach this, however, alongside some references that will help you get started.
Basic Steps
- Identify that configuration data is missing
- Redirect to a setup page
- Collect the missing configuration data from the user
- Save the missing configuration data to your configuration store
- Redirect back to the original
Addressing all of these is more involved than any one Stack Overflow answer, but I can provide some pointers that will help with the most likely stumbling blocks.
Intercepting Requests
The first requirement is to detect whether configuration data is missing and to redirect to a setup page. This is typically done via some type of ASP.NET Core Middleware or Filter. Middleware are a bit more flexible as they can happen earlier and cover a broader scope of requests, while filters are easier to write as they have easy access to more contextual information about the request.
Resource Filters
A common approach to this is to create an ASP.NET Core Resource Filter or that checks for the presence of key configuration resources—such as a connection string—and, if they’re not present, redirect the user to a setup page. A similar approach can be taken with ASP.NET Core Middleware.
Exception Filter
Another approach is to throw e.g., a custom UnconfiguredApplicationException at every point where you need a configuration variable, but one isn’t present. Then, handle that exception globally using a custom Exception Filter, a custom Middleware component, or the existing UseExceptionHandler middleware. This requires more code since you must add the check at every point where you depend on configuration data, but prevents the need to proactively validate your configuration data for every request.
Implementation
The Microsoft documentation for filters offers some good guidance for the basic construction of Resource and Exception Filters. It also includes an example for an Action Filter, which can be added to any controller action for a more granular approach.
Detecting Configuration
How you detect an unconfigured app depends on how and where you store your configuration variables. The most common approach is to use an IConfiguration provider, such as appsettings.json, which can be combined with the Options Pattern. The ASP.NET Core Filter documentation offers a basic example of an options object (PositionOptions), mapped to an IConfiguration provider (appsettings.json), and configured using ASP.NET Core's Dependency Injection container.
Setup Page
Since this is typically part of a redistributable library, your setup page will probably be handled via a Razor Class Library, which allows you to package Razor views and other assets with your library (e.g., as a NuGet package). Usually, you collect the information from the user that is specific to their installation—such as username, password, and server name—and then use that to establish a connection string, which you then save to the configuration source.
Detecting Database
If you don't know the type of database, you'd normally ask as part of the setup page. Though, I suppose, you could also loop through each driver, trying to connect based on the credentials provided, in order to determine which one is successful. But the person configuring the app should probably know what type of database server they installed!
Conclusion
As mentioned at the top, this is a really broad question, and there are a lot of moving parts. This should help get you started, though, and familiarize you with the main components involved in this process, such as ASP.NET Core Filters, the ASP.NET Core Configuration System, &c.
PostConfigure<TOptions>methods for configuration validation. andrewlock.net/…