Externalize the Configuration (appSettings.json) file in ASp.NET Core 3.1 - asp.net-core

I have developed a default Web API project using .Net Core Web API template. I would like to externalize the appSettings.json file to some other location instead of the default root location.
Below is the piece of code that I'm using.
I'm not able to read the settings from the Config file. Is there a way to retrieve them?
Thanks,
Praveen

Related

Upload large file .net core 3.1 api using iis without changing web.config

Is there any solution to uploading files larger than the default without changing the web.config?
On the documentation https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1, it says to change the web.config and to setup IISServerOptions.MaxRequestBodySize to our value, but in my current project i can't change the web.config. Is there any other solution?

Machine key implementation in .NET Core 3.1

In ASP.NET we would normally add a machine key to the web.config like this -
<machineKey validation="HMACSHA512" decryption="AES" validationKey="********" decryptionKey="******" />
Can someone please advises me how can we do the same in ASP.NET Core 3.1?
To enable SSO (Single Sign On) for multiple web applications on your site make sure that in the Startup.cs of all your web applications you add DataProtection and set the same ApplicationName, save the keys to the same directory and use the same cookie name like this:
services
.AddDataProtection()
.SetApplicationName("MyWebSite")
.PersistKeysToFileSystem(new DirectoryInfo(#"C:\MyWebSite-keys"));
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = "/login/login";
options.Cookie.Path = "/";
options.Cookie.Name = "MyWebSite-Login";
});
machineKey is no longer used in Core, instead of validationKey and decryptionKey only the masterkey will be used and will be created automatically in the directory you specify.
After this, after logging in application A and requesting application B the user will remain logged in.
You will need the install this package: Microsoft.AspNetCore.DataProtection.SystemWeb
It neeeds to be ASP.NET 4.5.1+.
Read more here: Replace the ASP.NET machineKey in ASP.NET Core
In Asp.net Core Application, you can implementation the <machineKey> element using the Data Protection API.
To use the Data Protection API in asp.net core application, first, install the package Microsoft.AspNetCore.DataProtection.SystemWeb. You might meet a warning "Warning NU1701...This package may not be fully compatible with your project.", on my side, this warning will not influence us to use this package.
[Note] The new data protection system can only be installed into an existing ASP.NET application targeting .NET 4.5.1 or later. Installation will fail if the application targets .NET 4.5 or lower.
Then, register the Data Protection service in the Startup.cs file, like this:
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(#"D:\temp\temp-keys\"));
After running the application, the generate file content as below:
The above code was configured a file system-based key repository to store the machine key, you can also use Azure Storage, Redis, Registry or Entity Framework Core. More detail information, check the Key storage providers in ASP.NET Core

Running .net application next to asp core app on IIS

I am trying to get my code to run alongside, in process, with an asp core web site.
With .net framework there is an option to create a module. Then, in order to "inject" that module so that it is run when the site runs all I need to do is add the module to web.config or launch it from a .cs file from \app_code.
With asp core, there is a concept called middleware but in order to add a middleware, the user has to write it into their startup code.
I need a way to run my .net core code when the site has started (first page accessed) without requiring the user to change their code to do so. Changing config files after deploy is OK but not compiled files.
Anyone know how to do this?
Thanks.
I see 2 options here:
Hosting startup assemblies
An IHostingStartup (hosting startup) implementation adds enhancements
to an app at startup from an external assembly. For example, an
external library can use a hosting startup implementation to provide
additional configuration providers or services to an app.
IIS modules with ASP.NET Core - you may still be able to inject your module in case you run the app on IIS. Add a web.config manually into the root directory and configure your module in there.

Read configuration from a specific file - Web.config or appsettings.json based on the calling web application

I have a monolith project and I'm trying to convert one Web Application from ASP.NET to ASP.NET Core with Full Framework.
As you can see from the figure, both Web Applications are referencing Class Library 3. CL3 contains Business Logic but also logic about how to read specific configuration (till now from Web.config file).
Since the way to read configuration has changed in ASP.NET Core (appsettings.json Environment based) I have to rewrite the part that reads the configuration from CL3.
Is it possible a scenario like this?
#IF CALLED FROM ASP.NET
RETRIEVE CONFIGURATION FROM WEB.CONFIG
#ELSE (CALLED FROM ASP.NET CORE FULL FRAMEWORK)
RETRIEVE CONFIGURATION FROM APPSETTINGS.JSON
If you are refactoring, try to move configuration reads out of the shared DLLs and provide a clean abstraction that does not have hidden dependencies on framework-specific assets.
Your ASP.NET Core Application and classic ASP.NET Applications could create a config based on their config system (you could provide two different config implementations) and then use your CL3's classes and pass in configuration objects.
Alternatively, you could use a ConfigurationBuilder to read AppSettings and connection strings from appsettings.json and appsettings.{Environment}.json and make it available through the classic ConfigurationManager API. See this blog post for details on how to do this. This can be extended to configuring custom configuration sections (classic .NET) from json files.

ASP.NET Core Application Initialization on Azure

I'm trying to implement Application Initialization (warm up) on an ASP.NET Core web app running on Azure, as described for IIS 8.
The way it's described is changing web.confg to enable and configure it.
But in asp.net core we (almost) don't have web.config.
So, how do we configure Application Initialization (or any other feature that require
changing web.config)?
I finally figured out, for this question and any other that requires IIS to be configured through web.config:
It's not true there isn't web.config anymore. It's true that it doesn't play any role in the asp.net core app, but you can find a small web.config file in wwwroot project folder, which is what tells IIS how to handle the asp.net core app.
So for implementing this, or for example urlrewrite, you just need to add IIS configuration to that web.config file as it's being deployed as part of the application.