Demystify Publish folder of an ASP.Net Core MVC Application

In this tutorial we are going to demystify the contents of Publish folder of an ASP.Net Core MVC application. To host an ASP.Net Core Application, we publish it using dotnet publish command of Dotnet CLI. Publish folder is created as an output of dotnet publish command and it contains all the necessary dependencies along with project artifacts for successful project hosting.

NOTE: Updated this tutorial on 2/27/2017 with MSBuild based Dotnet SDK.

Create an ASP.Net Core Application using VS 2017. I am using following version of Dotnet SDK which is based on MSBuild/CSProj (remember that previous versions of ASP.Net Core are based on Project.json/XProj.).

 image

We will publish the application using dotnet publish.

image

Publish folder will have all the artifacts required for running the application (below image has been trimmed).

image

Publish folder primarily contains -

  1. Views
  2. wwwroot
  3. refs
  4. appsettings.json
  5. ApplicationName.deps.json
  6. Program Debug Database (PDB)
  7. ApplicationName.runtimeconfig.json
  8. web.config
  9. Dependent DLL’s along with application DLL.
  10. Runtimes

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. And as usual, _ViewStart.cshtml is used to hold common code which will fire for each view.

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

refs

This folder is created with all reference assemblies from ApplicationName.deps.json file (check below for explanation about this file). This folder is primarily used by MVC to determine the list of dependencies that reference MVC libraries on startup and to compile Razor views at runtime.

appsettings.json

This file 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": "b57ec97a-f3dd-44a4-ba8f-e3a57743d3c"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

ApplicationName.deps.json

ApplicationName.deps.json is used to compile Razor views at runtime (typically for ASP.Net Core MVC applications). This file is created when preserveCompilationContext is set to true in CSProj file. Below is the the outline of the file (I never copy pasted the file because it is huge). compilationOptions holds the framework  which is used to build under specific configuration (below image shows we have used NETCOREAPP1_0 under DEBUG configuration). runtimeTarget property contains the name of the target that should be used by the runtime. Targets section contains entries which are collection of libraries which are dependencies. A specific target is used compile or run (based on the runtime identifier) the application with a certain framework on a platform. Libraries section contains a union of all the dependencies found in the various targets, and contains common metadata for them.

image

Program Debug Database (PDB)

The Program Debug Database file contain debug information to enable easier debugging. PDB files helps in navigating to the correct source file and line of code in case of exceptions. There is no relevance for this file in hosting and we can safely ignore it.

ApplicationName.runtimeconfig.json

This file specifies parameters which are passed to the runtime during initialization. It includes the details of framework to be used when application is being activated. ConfigProperties is used to configure the runtime for example whether to use GC or not, and if it is used then can it be used in background mode etc. One more option is to specify applyPatches which is used to load framework which is equal to or greater than semVer of specified version (if 1.0.1 is specified as framework version and applyPatches to true, then anything greater than or equal to 1.0.1 would be used but not less than the specified version). If applyPatches is false, then host is going to use strict version of framework.

web.config

When we host ASP.Net Core application in IIS, IIS uses AspNetCoreModule which will execute (through dotnet run command of Dotnet CLI) the external ASP.Net Core console application. Dotnet run command will load the Kestrel server and serves the initial request. For subsequent requests, AspNetCoreModule redirects (making IIS as a reverse proxy) to ASP.Net Core Application. AspNetCoreModule is going to be configured via web.config of ASP.Net Core Application. In web.config, we are going to mention the DLL which needs to be run under dotnet command. The other parameters include enabling the log output and log file. There is no need of manual work in generating this web.config, dotnet publish will take care of it.

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

Dependent DLL’s along with application DLL

All the dependent libraries which are primarily required by the application are placed along with the above mentioned files and folders. The compiled ASP.Net Core MVC application DLL is also placed in the publish folder.

Runtimes

Runtimes folder will hold all the necessary runtimes which application can target. This is helpful in created self contained deployment package. The individual runtime’s under runtimes folder, will hold the necessary .Net core runtime DLL’s and runtime binaries.

That’s it for now, we have covered all the important files and folders of publish folder of an ASP.Net Core MVC Application. Happy Coding and Stay Tuned!!!

You may also like...