Demystify ASP.Net Core 1 Visual Studio Project

In Getting Started with ASP.Net Core 1 Application, we saw how to create a new ASP.Net Core 1 Application and run it. In this tutorial we are going to understand the Visual Studio Project structure. There are lot of changes to the Core Project structure when compared to traditional ASP.Net MVC4 Project. We will go by each folder and file to understand its relevance in the project context.

Lets get started by looking at the project structure.

image

Solution Items folder and Global.json:

This folder is not specific for a project, but it is relevant for entire solution and all its projects. The only file which resides in this folder by default is global.json, which is used to configure the entire solution.

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003131"
  }
}

Projects setting holds the details of folders which contain actual projects, usually we follow the convention by putting all source under src folder and test projects under test folder.

SDK setting sets which version of .Net (along with architecture and runtime) should be used to execute all the projects in the solution.

AspNetCoreApp:

This is the actual ASP.Net Core 1 Project. One interesting change to identify is that the project file is not .csproj, but it is .xproj. In ASP.Net Core 1 Application, xproj along with project.json replaces most of the csproj configuration and functionality (In future versions, xproj/project.json is going to be replaced with csproj/MSBuild. We will understand about this change in next tutorials). Typically xproj contains all the information which is necessary for MSBuild and project.json contains all the information related to dependencies and frameworks required to run the application.

Properties and launchSettings.json:

image

Project level environment variables can be set by double clicking Properties node. This is helpful in setting different deployment variables through which certain operations can be performed based on environment. Any changes to project settings will be saved to launchSettings.json file (described below).

image

When we expand properties node, we have launchSettings.json, which contains different profiles for project launch. We can select any of the profile either from VS or from Dotnet CLI to run that specific project. Also it contains IIS settings which will be used when app hosted in IIS.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:55813/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "AspNetCoreApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

image

References:

References node displays all the dependencies of the project (just like in traditional ASP.Net MVC4 Project). These dependencies are pulled out from project.json file along with the specific targeted framework. All the dependencies will be restored initially when project is created and loaded for the first time, next time whenever project.json changes, dependencies will be restored automatically.

image

ServiceReferences:

Visual Studio Add Connected Service Dialog is used to automate the manual process of connecting to services (cloud, 3rd party, WCF etc.). All connected services of ASP.Net Core 1 Application are listed in ServiceReferences node. Adding a connected service will add dependencies to project.json and will trigger nuget restore automatically.

image

wwwroot:

wwwroot folder contains all static files of the project. This includes HTML, Image, JavaScript and CSS files. These files will be served to user directly. Custom JQuery files will go into js folder and all JQuery Library files will go to lib folder. _references.js file is used by VS for intellisense.

image

Dependencies:

Dependencies node primarily deals with client side dependencies (JQuery, CSS, Fonts etc.) especially with bower and npm. By default bower has been configured with ASP.Net Core 1 Application. To add npm support, we can add new item, NPM Configuration file and there by add dependencies (I have added npm for demonstration).

image

Controllers:

Controllers folder will hold all the MVC/Web API controllers in ASP.Net project. It is as same as traditional ASP.Net MVC4 Project.

Views:

This folder holds all the cshtml files. It follows the same convention as of traditional ASP.Net MVC4 project, by placing views corresponding to a controller under the same folder with controller name. Also this folder holds commonly shared views (like partials) in Shared folder. This folder contains a new file called _ViewImports.cshtml, this file serves as a common place to add all namespaces which are used by all views along with taghelpers.

image

appsettings.json:

This file can be used to store custom key value pairs (just like appSettings in web.config file of traditional ASP.Net Projects) for the project. This file is also used to hold connection strings, log settings etc.

{
  "ApplicationInsights": {
    "InstrumentationKey": "de312d15-591f-44b9-1f8443d54e3f"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

bower.json:

Client side asset files (like js, css, images, html etc.) are managed through bower.json. By default, Bootstrap and JQuery files are loaded. .bowerrc file holds the location (typically wwwroot/lib) where bower needs to save the downloaded packages.

{
  "name": "asp.net",
  "private": true,
  "dependencies": {
    "bootstrap": "3.3.6",
    "jquery": "2.2.0",
    "jquery-validation": "1.14.0",
    "jquery-validation-unobtrusive": "3.2.6"
  }
}

bundleconfig.json:

ASP.NET Core 1 Application includes BundlerMinifier.Core which will help bundling and minification of JQuery and CSS files. bundleconfig.json is the file where we specify different input files and corresponding output file.

dotnet bundle is the command which we can use to trigger the bundle and minification operation. This command is part of prepublish commands of project.json.

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optinally generate .map file
    "sourceMap": false
  }
]

Program.cs:

Program.cs is the starting point of ASP.Net Core 1 Application. Previously Static Void Main() entry point is part of Startup.cs, but now they made it into Program.cs, making the ASP.Net core 1 App a truly console application.

NOTE: Prior to Core 1 (I mean RC1 days), ASP.Net Core App is a class library and DNX along with ASP.Net Hosting libraries find and execute Startup.cs to boot the application. But in Core 1, ASP.Net Core App is a console app which will trigger the ASP.Net Hosting libraries tasks of starting the application.

using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

project.json:

project.json is the file which will hold all the important information about the project like dependencies, runtimes, build options, runtime options, compile options, publish options, pre publish and post publish events etc.

More detailed explanation of project.json will be published through a different tutorial.

project.lock.json:

In ASP.Net Core 1 Application, all the dependencies are managed through project.json. project.lock.json includes complete list of all of the nuget packages which were in use. This file holds more detailed information about each and every package with specific version (project.json might have wildcard version) which is installed along with all the dependencies that package got. Abstract of Microsoft.Extensions.Logging package is shown below.

This file will be written with dependency graphs at the time of restoring of packages The main purpose of this file is to track and analyze the dependency graph for a successful project execution (remember in ASP.Net Core 1 Project bin folder in empty and all the relevant dependencies are pulled at runtime from this file) and to make subsequent restore and project execution process faster.

"Microsoft.Extensions.Logging/1.0.0": {
  "type": "package",
  "dependencies": {
    "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0",
    "Microsoft.Extensions.Logging.Abstractions": "1.0.0",
    "System.Threading": "4.0.11"
  },
  "compile": {
    "lib/netstandard1.1/Microsoft.Extensions.Logging.dll": {}
  },
  "runtime": {
    "lib/netstandard1.1/Microsoft.Extensions.Logging.dll": {}
  }
}

Startup.cs:

Startup.cs is the entry point which configures the environment for ASP.Net Core 1 Application. In its constructor, it configures the configuration of the application. In its ConfigureServices method, framework services (for example EF, MVC, Authentication etc.) and custom services (business specific) will be added and resolved using Dependency Injection. There is one more method, Configure, which is used to build the ASP.Net HTTP Pipeline with middlewares.

More detailed explanation of Startup.cs will be published through a different tutorial.

web.config:

ASP.Net Core 1 Applications use ASP.Net Core Module (native IIS Module) for hosting in IIS (this new module got ASP.Net features when compared with HTTP Platform Handler). The configuration for new ASP.Net Core Module lies within web.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

Breaking Changes to expect in new future:

  1. project.json is going to go away and configuration is likely to be part of .csproj is going to be taken care by MSBuild.
  2. .xproj is going to be made back to .csproj.

I am going to write up a different article on the new breaking changes from ASP.Net Core 1 to forward. Happy coding and Stay tuned!!!

You may also like...