Automatically modify Startup.cs and appsettings.json files when Nuget package is installed

Ihor Kuliiev 21 Reputation points
2021-11-08T12:45:34.27+00:00

Hello!

I am going to create a Nuget package to use within our organization's ASP.NET applications. I need to find out whether it is possible to automatically add some lines of code into the Startup.cs file and a new section into the appsettings.json file once the package is installed.

Thank you in advance!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-11-09T02:18:44.48+00:00

    Hi @Ihor Kuliiev ,

    I am going to create a Nuget package to use within our organization's ASP.NET applications. I need to find out whether it is possible to automatically add some lines of code into the Startup.cs file and a new section into the appsettings.json file once the package is installed.

    It is not possible. As we all known, a Nuget package contains compiled code (as DLLs) along with other content needed in the projects that consume these packages. Before the Nuget package publish to the nuget.org, it has been compiled. So, when installed them, it will not update the Startup.cs file and the Appsettings.json file.

    And most importantly, this is not safe to automatically modify the configuration when installed the package.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful