ASP.NET MVC 6 New Folder Structure

Introduction

This article will describe a new concept, which is introduced in ASP.NET MVC 6.

ASP.NET 6 introduces a few concepts, which didn’t exist in the previous versions of ASP.NET. Rather than working with web.config, packages.config and a variety of project properties stored in the .csproj/.vbproj file, the developers can now work with specific files and the folders devoted to the specific purposes.

New Folder Structure

New Folder Structure

Startup.cs

In previous versions of ASP.NET for configuration stuff, we had to use web.config file or *.config files. Good to hear web.config is gone now. Configuration itself can now be configured in ASP.NET MVC 6. This is very good news because creating the configuration is a huge pain for the developers.

Microsoft is now supporting JSON, XML and INI files for configuration and those you can easily create your own. In MVC 6, “configuration” will mostly be done with code found in your Startup.cs file, anything that you want to enable in your application is done in this class. Any package that is installed in your application, you will need to opt-in to use it in your Startup.cs file. In some cases however, a configuration file might need to be updated/transformed… this could be due to an upgrade of a package.

The good news is that the configuration sources in MVC 5 are writable (IConfigurationSource), which means during the startup or first access to your config, you can detect what needs to be updated to support your new version, make the updates in the code and commit (ICommitableConfigurationSource) the changes.
  1. public Startup(IHostingEnvironment env) {  
  2.     // Set up configuration sources.   
  3.     var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddJsonFile($ "appsettings.{env.EnvironmentName}.json", optional: true);  
  4.     if (env.IsDevelopment()) {  
  5.         // For more details on using the user secret store   
  6.         // see http://go.microsoft.com/fwlink/?LinkID=532709   
  7.         builder.AddUserSecrets();  
  8.         // This will push telemetry data through Application Insights pipeline faster,   
  9.         // allowing you to view results immediately.   
  10.         builder.AddApplicationInsightsSettings(developerMode: true);  
  11.     }  
  12.     builder.AddEnvironmentVariables();  
  13.     Configuration = builder.Build();  
  14. }  
project.json

project.json is another json file. It is used to define the projects' Server side dependencies and other project specific information such as usersecretsid to identify the uniqueness of your Application, version of the Application and so on.

Server side dependencies refer to an installed NuGet package or to another project. Each of the entry package versions can be specified specifically. This is the best feature it has provided to avoid pulling the major version to the minor version update. This was a huge pain, while deploying the project.
  1. {  
  2.     "userSecretsId""aspnet5-WebApplication1-8479b9ce-7b8f-4402-9616-0843bc642f09",  
  3.     "version""1.0.0-*",  
  4.     "compilationOptions": {  
  5.         "emitEntryPoint"true  
  6.     },  
  7.     "dependencies": {  
  8.         "EntityFramework.Commands""7.0.0-rc1-final",  
  9.         "EntityFramework.MicrosoftSqlServer""7.0.0-rc1-final",  
  10.         "Microsoft.AspNet.Authentication.Cookies""1.0.0-rc1-final",  
  11.         "Microsoft.AspNet.Diagnostics.Entity""7.0.0-rc1-final",  
  12.         "Microsoft.AspNet.Identity.EntityFramework""3.0.0-rc1-final",  
  13.         "Microsoft.AspNet.IISPlatformHandler""1.0.0-rc1-final",  
  14.         "Microsoft.AspNet.Mvc""6.0.0-rc1-final",  
  15.         "Microsoft.AspNet.Mvc.TagHelpers""6.0.0-rc1-final",  
  16.         "Microsoft.AspNet.Server.Kestrel""1.0.0-rc1-final",  
  17.         "Microsoft.AspNet.StaticFiles""1.0.0-rc1-final",  
  18.         "Microsoft.AspNet.Tooling.Razor""1.0.0-rc1-final",  
  19.         "Microsoft.Extensions.CodeGenerators.Mvc""1.0.0-rc1-final",  
  20.         "Microsoft.Extensions.Configuration.FileProviderExtensions""1.0.0-rc1-final",  
  21.         "Microsoft.Extensions.Configuration.Json""1.0.0-rc1-final",  
  22.         "Microsoft.Extensions.Configuration.UserSecrets""1.0.0-rc1-final",  
  23.         "Microsoft.Extensions.Logging""1.0.0-rc1-final",  
  24.         "Microsoft.Extensions.Logging.Console""1.0.0-rc1-final",  
  25.         "Microsoft.Extensions.Logging.Debug""1.0.0-rc1-final",  
  26.         "Microsoft.VisualStudio.Web.BrowserLink.Loader""14.0.0-rc1-final"  
  27.     },  
  28.     "commands": {  
  29.         "web""Microsoft.AspNet.Server.Kestrel",  
  30.         "ef""EntityFramework.Commands"  
  31.     },  
  32.     "frameworks": {  
  33.         "dnx451": {},  
  34.         "dnxcore50": {}  
  35.     },  
  36.     "exclude": ["wwwroot""node_modules"],  
  37.     "publishExclude": ["**.user""**.vspscc"],  
  38.     "scripts": {  
  39.         "prepublish": ["npm install""bower install""gulp clean""gulp min"]  
  40.     }  
  41. }  
appsettings.json

As we see, web.config is replaced with JSON files, appsettings.json file json file in which we can mention the database connection string. In web.config file, we use <ConnectionString> to configure the connection string. Here, it will create in JSON format string to mention the connection string.
  1. {  
  2.     "ApplicationInsights": {  
  3.         "InstrumentationKey"""  
  4.     },  
  5.     "Data": {  
  6.         "DefaultConnection": {  
  7.             "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-MVC6VNext-d6155af6-412a-4c71-8378-ae3c4ddbe2ed;   
  8.             Trusted_Connection = True;MultipleActiveResultSets = true "   
  9.         }  
  10.     },  
  11.     "Logging": {  
  12.         "IncludeScopes"false,  
  13.         "LogLevel": {  
  14.             "Default""Verbose",  
  15.             "System""Information",  
  16.             "Microsoft""Information"  
  17.         }  
  18.     }  
  19. }  
global.json

global.json is used to configure the solution. .SIN in the past is replaced with global.json file now. By default, the projects and sdk are two sections mentioned in the file.

The version is important because your computer can have multiple versions of dnx.exe. You can see%USERPROFILE%\.dnx\runtimes using this command. The multiple version of dnx is installed. The "sdk" part ofglobal.json defines the version of dnx.exe from one of the runtimes, which you installed.

It's important to understand about  the "projects" part of global.json, which will be scanned by all the sibling folders on any level under every directory.
  1. {  
  2.     "projects": ["src""test"],  
  3.     "sdk": {  
  4.         "version""1.0.0-rc1-update1"  
  5.     }  
  6. }  
wwwroot Folder

The wwwroot folder is new in MVC 6 to store all of the static files in your project. Any files including HTML files, CSS files, image files, and JavaScript files, which are sent to the user's Browser should be stored inside this folder.

The code files should be placed outside wwwroot, including C# files and Razor views. Having a wwwroot folder keeps a clean separation between the code files and static files. It brings clarity to the items, which will be sent to the Server and the items, which should remain on the dev machine.

wwwroot Folder

Dependency Folder (Client Side Dependency Management)

In this folder, we can find two more folders, which are-
  1. Bower
    Today, most of the Web Applications are using JavaScript and CSS framework such as jQuery, bootstrap, AngularJs, etc. Front-end developers working with other frameworks are more likely to use a system called Bower, which is a package manager specifically for managing client-side libraries which are located on GitHub. Bower was developed by the team at Twitter (who also produced Bootstrap), and is a Node.js Application.

  2. NPM
    Previous versions of ASP.NET included a framework for minifying and bundling client-side script and style sheets files - System.Web.Optimization. As its name implies, it is dependent on System.Web, and therefore hasn't been included as part of ASP.NET 5. Rather than reinventing the wheel a second time, ASP.NET team decided to incorporate a popular Node.js application to perform these tasks - Gulp.

    Gulp is variously described as a build system or a task runner that you can use to automate the copying of files, bundling and minification, compiling LESS or SASS to CSS, etc. as part of your build. There are other Node.js task runners available, but this particular tool has been chosen by the ASP.NET team as one of the options to include in Visual Studio. It uses streams when handling files, which is a lot more efficient than creating temporary copies. You obtain Gulp using npm - a package manager bundled with Node.js.

    NPM

References (Server Side Dependency Management)

The References folder, shown within Solution Explorer in Visual Studio, details the server-side references for the project. It should be familiar to ASP.NET developers, but it has been modified to differentiate between references for different framework targets, such as the full DNX 4.5.1 vs. DNX Core 5.0. Within each framework target, you will find individual references, with icons indicating whether the reference is to an assembly, a NuGet package, or a project. These dependencies are checked at compile time, with missing dependencies downloaded from the configured NuGet package source.

References