Set the environment for an ASP.NET Core MVC web application

When the ASP.NET Core MVC web application starts, reads the name of the environment for an environment variable of the host computer. The compiled application contains the configuration for all supported environments, making possible to build and deploy the same artifact in every environment.

If the ASPNETCORE_ENVIRONMENT environment variable is not defined, the Production environment settings are loaded.

In Visual Studio the web application project sets the value on the Debug tab.

To set the environment on the server


To set the value globally that is preserved after restart

Command prompt

  1. Open the command prompt window and execute
    setx ASPNETCORE_ENVIRONMENT Development /M


  1. Open a PowerShell window and execute
    [Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development", "Machine")


  1. Add to the .bashrc or .bash_profile file
    export ASPNETCORE_ENVIRONMENT=Development


Configure the ASP.NET Core MVC web application

Create the environment-specific appsettings.json files

  1. In the web application project click the arrow next to the appsettings.json file to reveal the Development configuration file
  2. Right-click the appsettings.Development.json file and select Copy

  3. Right click the web application project and select Paste

  4. Rename the appsettings – Copy.Development.json file to appsettings.Production.json

  5. The file will automatically move under the appsettings.json file

Edit the configuration files

  1. Open the appsettings.Production.json file and add the connection string
        "ConnectionStrings": {
            "DefaultConnection": "Server=MY_DATABASE_URL;Database=MY_DATABASE_NAME;Username=MY_USERNAME;Password=MY_PASSWORD"


Start a new Spring MVC Java Web application development in NetBeans

When you create your first Spring MVC Java web application in the base installation of NetBeans follow the steps below

Create a project

  1. In the File menu select New Project
  2. Select the Java Web category, select the Web Application project, and click Next,
  3. Enter the name of the project and click Next,
  4. If this is the first Java web application you create in NetBeans, most likely there is no web server for Java is installed on your workstation. If this is the case, click the Add… button,
  5. Keep the GlassFish Server selected and click Next,
  6. Select the I have read… checkbox and click the Download Now… button,
  7. Keep the latest version selected and click OK,
  8. Make sure the I have read… checkbox is still selected, and click Next,
  9. Click Finish to register the domain on your workstation,
  10. The Context Path shows the address for your application. Click Finish again to continue.
  11. Select the Spring Web MVC framework and click Finish.
  12. If a library is missing from your workstation, a message pops up. Click the Resolve Problems… button for instructions,
  13. On the Resolve Project Problems window click the Resolve… button,
  14. Download the missing library to your workstation. In this case, navigate your web browser to
  15. Download the “javaee-endorsed-api-7.0.jar” library
  16. Create the folder C:\Users\YOUR_USER_NAME\AppData\Roaming\NetBeans\8.2\modules\ext\javaee7-endorsed
  17. Extract the “javaee-endorsed-api-7.0.jar” file into the new folder,
  18. On the Ant Library Manager window click the New Library… button,
  19. Enter “javaee-endorsed-api-7.0” into the Library Name field and click OK,
  20. Click the Add JAR/Folder… button,
  21. Navigate the to extracted JAR file at “C:\Users\YOUR_USER_NAME\AppData\Roaming\NetBeans\8.2\modules\ext\javaee7-endorsed” and click the Add JAR/Folder button,
  22. On the Ant Library Manager window click the OK button,
  23. On the Resolve Project Problems window click Close.

Start the application

  1. In the Project list right-click the web application project and select Run
  2. The Output window should display a similar message,
  3. Open a web browser and navigate to http://localhost:8080/MY_PROJECT_NAME, to display the index page. The server is case sensitive, so make sure the spelling of the project name is correct.


The application configuration files

With the default settings the index.jsp composes the index page. It is in the Web Pages/WEB-INF/jsp directory.

The web.xml file in the Configuration Files directory points to the first Java Server Pages file: redirect.jsp

The redirect.jsp file is located in the Web Pages directory

The Dispatcher

The dispatcher-servlet.xml file contains the mapping between the HTML file, controller, and view.

<property name="mappings">
    <prop key="index.htm">indexController</prop>

The bean section specifies the name of the view for the controller.

 The index controller.
 <bean name="indexController"
       p:viewName="index" />

Web Application Build and Deployment

To generate a war file of your web application

  1. In the Project Files directory right click the pom.xml file and select Run Maven > Goals
  2. In the Goals field type
    compile war:war

    and click OK

  3. NetBeans creates the war file in the target directory.


Configure NetBeans for Java Web development

The base installation of NetBeans does not include the web development category.


To add the Java web development tools to NetBeans

  1. Start the NetBeans IDE,
  2. In the Tools menu select Plugins,
  3. On the Available Plugins tab select the Java EE Base plugin and click the Install button,
  4. If any of the installed plugins is missing the valid signature, you need to click the Continue button to allow the installation,
  5. For the plugin installation, you need to restart the NetBeans IDE,
  6. During the next start, NetBeans installs the new plugins,
  7. The Java Web category is now available.

How to add the Config folder to a Microsoft Visual Studio project even if the folder is not in the project directory

Corporations maintain multiple server environments for development, testing and production purposes.
All of these environments require specific settings to connect to the databases and other resources. When the build master builds the application for the specific environment the build process has to select the appropriate configuration and build it into the installer file for deployment.
It is a good practice to store these environment specific config files in a folder that also designates the Visual Studio version.

Console applications and Windows services

For console applications and Windows services place this folder into the “Source” folder.

visual studio add folder to project folder tree


In Microsoft Visual Studio you can add a folder to a project, but it will be created in the directory where the project file is located. To add a folder to a project when the folder is not in the project directory we have to manually edit the project file. In this example we will add a folder to our main project that contains the environment specific config files. The “Config_2012” folder is located in the “Source” folder outside of the “Project_2012” project directory.

The following steps will add the “Config_2012” folder to the project

  • Create the “Config_2012” folder in Windows Explorer
  • Add the environment specific config files to it
  • Add the folder to source control
  • Open the solution in Visual Studio
  • Right click the main project and select Unload Project
  • Right click the main project again and select Edit…
  • Add the following to the project file

<None Include="..\Source\Config_2012\Dev.config">
<None Include="..\Source\Config_2012\Local.config">
<None Include="..\Source\Config_2012\Prod.config">
<None Include="..\Source\Config_2012\QA.config">
<None Include="..\Source\Config_2012\Staging.config">
<None Include="..\Source\Config_2012\UAT.config">


  • Save the project file
  • Right click the main project file and select Reload Project

The “Config_2012” folder will appear in the Solution Explorer and the config files will be added as linked files.

visual studio add folder to project

Web applications and web services

For web applications and web services place the Config folder into the web application folder

web application folder structure config_2012 folder highlighted

To add the config folder and files to the web application

  • Open the web solution in Visual Studio
  • Select the web project
  • Click the Show All Files button on the Solution Explorer toolbar

visual studio show all files button


  • The Solution Explorer will display all files in the web application folder

web application solution explorer show all files config_2012 folder and files


  • Right click the Config_2012 folder  and select Include In Project

web application solution explorer show all files config_2012 folder include in project


  • Select all the config files in the Config_2012 folder
  • Right click the selection and select Properties

web application solution explorer show all files config_2012 folder files properties


  • Set the Build Action to None

web application file properties build action none


  • Click the Show All Files button on the Solution Explorer toolbar again to hide the excluded files

visual studio show all files button