Click here to Skip to main content
15,881,789 members
Articles / Web Development / ASP.NET / ASP.NET Core

Converting ASP.NET 5 Beta 8 App to ASP.NET Core RC2

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
6 Jun 2016CPOL6 min read 12.6K   2  
This article will walk you through on converting your existing ASP.NET 5 Beta8 App to ASP.NET Core RC2.
Quote:

UPDATE: Upgrading ASP.NET Core RC2 to RTM

Introduction

This article will walk you through on converting your existing ASP.NET 5 Beta8 App to ASP.NET Core RC2.

Background

Late last year, I wrote an article about "Upgrading Existing ASP.NET 5 previous versions to Beta8". Since then I stick to using Beta 8 version for building ASP.NET Core app prototypes. Besides being lazy, it was my intent not to upgrade my existing projects to RC1 because I know that RC2 will be released in the near future.

Few days ago Microsoft announced the release of ASP.NET Core RC2. This release succeeds the ASP.NET 5 RC1 release and features a number of updates to enhance compatibility with other .NET frameworks and an improved runtime.

A key change that occurred between Betas and RC2 is the introduction of the .NET command-line interface (CLI). This tool replaces the dnvm, dnx, and dnu utilities with a single tool that handles the responsibilities of these tools. To learn more about the .NET CLI, check out Announcing .NET Core RC2 and .NET Core SDK Preview 1.

If you are working with ASP.NET 5 Beta 8 versions up to this moment and wanted to migrate your app to the latest release which is RC2 as of this writing then this article is for you. Though this article doesn’t list down all the changes, this would somehow list the changes for the most basic part so your app can run under RC2 version.

Using the code

Warning: Spoiler Ahead!

There will be big changes under the hood with the release of ASP.NET Core RC2, so expect a longer list for this conversion. :)

Let's get Cracking!

First thing you need to do is upgrade your Visual Studio 2015 with Update 2. You can check the release and download the update for Visual Studio here.

Once the setup and configuration is applied in Visual Studio then download the ASP.NET Core RC2 here.

After the installation, make sure to restart your machine to ensure updates will take effect.

In this conversion I’m going to use the ASP.NET MVC 6 project that I’ve demonstrated before. Now let’s start modifying.

global.json Changes

Change your global.json to this (if you have any):

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview1-002702"
  }
}

Two things we’ve changed there, first was to replace “sources” node to “projects” and then replaced the version from “1.0.0-beta8” to “1.0.0-preview1-002702” – the latest RC2 version as of this writing.

project.json Changes

dependencies

Add the runtime as a dependency:

JavaScript
"dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
},
  • Replace all “Microsoft.AspNet.” dependencies to “Microsoft.AspNetCore.
  • Then replace version suffix from “-beta8” to “-rc2-final”.
  • Note: if you are referencing “Microsoft.AspNet.Mvc": "6.0.0-beta8" then make sure to change the version to “1.0.0” so it would look like this"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final"
  • Next, change the name of all “Microsoft.Framework.” references to “Microsoft.Extensions.”.
  • If you are using SQL Server, you will need to change the reference of “EntityFramework.SqlServer” to “Microsoft.EntityFrameworkCore.SqlServer”.
  • After that, change existing assemblies with new names for example: "Microsoft.AspNet.IISPlatformHandler" to "Microsoft.AspNetCore.Server.IISIntegration", and "Microsoft.AspNet.Identity.EntityFramework" to "Microsoft.AspNetCore.Identity.EntityFrameworkCore"
  • Add the following configuration and diagnostics references:
JavaScript
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
frameworks

Change your frameworks section with the following new naming structure:

JavaScript
"frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
},
tools

You will need to rename the existing “command” section to “tools” and add the following tools config from the preview like this:

JavaScript
"tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
},
publishOptions

Next, remove the existing “bundleExclude” and “bundle” config section and replace them with the new publishOptions section like:

JavaScript
"publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
}

Add the buildOptions and runtimeOptions sections like so:

JavaScript
"buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
},

Setting the “preserveCompilationContext” to “true” will compile Views. This setting is important especially if you are using a strongly-typed view that needs to be compiled.

Add the new “Scripts” section:
JavaScript
"scripts": {
    "postpublish": [ "dotnet publish-iis 
        --publish-folder %publish:OutputPath% 
        --framework %publish:FullTargetFramework%" ]
}

Wrapping all Changes

Here’s my updated project.json file after putting together the changes:

JavaScript
{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis 
        --publish-folder %publish:OutputPath% 
        --framework %publish:FullTargetFramework%" ]
  }

}

Now save the file so it will restore the required packages for your app.

xproj File Changes

Unload your project first and then right-click on it to edit the project file. We will need to manually change some configuration in the file because your existing xproj file has references to the old Beta 8 tools which uses DNX. In this case we will need to change the string "DNX\Microsoft.DNX.Props" to "DotNet\Microsoft.DotNet.Props". Basically the “DNX” reference will now be changed to “DotNet”. So your import tag should now look like this:

XML
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />  

You also need to change the string "DNX\Microsoft.DNX.targets" to "DotNet.Web\Microsoft.DotNet.Web.Props". Your other import tag would now look like this:

XML
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" 
        Condition="'$(VSToolsPath)' != ''" />  

You may also need to change BaseIntermediateOutputPath and OutputPath to:

XML
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>  

And finally, add a TargetFrameworkVersion below the OutputPath element:

XML
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>

Now save your .xproj file and then reload your project in Visual Studio.

Code Changes

Adding Program.cs File

Adding this class is not really a requirement because you can just add an entry point to your existing startup.cs file. But just to follow the new project templates for RC2, I’ve decided to generate a separate file called “Program.cs” to put our main entry point – the static void Main() method.

The Program.cs file contains the Main method of an ASP.NET Core RC2 app, which is responsible for configuring and running the app. Here’s how it would look like:

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

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

            host.Run();
        }
    }
}
Modifying Startup.cs

Here are the changes for my Startup.cs file:

C#
public Startup(IHostingEnvironment env)  
{
    var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath);

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }
Renaming Namespaces

Since our project.json package references was changed then, we also need to change the namespaces that are being used. For example do a “find and replace” and then change “using Microsoft.AspNet” to “using Microsoft.AspNetCore”. Following are the affected changes in my project:

C#
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.AspNetCore.Mvc;

You also need to change the namespace reference of the following: 
"Microsoft.Framework.DependencyInjection" > "Microsoft.Extensions.DependencyInjection" "Microsoft.Data.Entity" > "Microsoft.EntityFrameworkCore"

launchSettings.json Changes

Under Properties, open up the launchSettings.json file. Find the “web” section and replace it with the new “DotNet” stuff below:

JavaScript
"MVC6Demo": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
}

The “ASPNETCORE_ENVIRONMENT” replaces the old “Hosting:Environment” and is the new environment variable for RC2.

You may also want to change the port number used in IIS Express. In my case I have this configuration:

JavaScript
"iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50295/",
      "sslPort": 0
    }
},

ViewComponent Changes

InvokeAsync now takes anonymous object instead of separate parameters. With this change you would need call @Component.InvokeAsync(“Name of View Component”,) from your View. In my previous example I needed to change this call:

Razor
@await Component.InvokeAsync("HeroList", "strength")

to this:

Razor
@await Component.InvokeAsync("HeroList", new { type = "strength" })

Now save and compile your project. Here’s a sample screenshot of the converted ASP.NET MVC 6 App to ASP.NET MVC Core RC2: 

Image 1

Figure 1: Output

If you are having issues migrating your App, then try to enable the UseDeveloperExceptionPage within your Configure() method to diagnose and troubleshoot issues.

C#
public void Configure(IApplicationBuilder app){
      app.UseDeveloperExceptionPage();
}

Note: I know there are few changes that I didn't cover in this article because I’m just basing a simple project to convert. To learn more about the RC2 conversion please refer the following articles below:

That’s it! I hope someone find this article useful.

Summary

In this article we've learned how to convert an existing AS.NET 5 Beta8 App to ASP.NET Core RC2.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
A code monkey who loves to drink beer, play guitar and listen to music.

My Tech Blog: https://vmsdurano.com/
My Youtube Channel: https://www.youtube.com/channel/UCuabaYm8QH4b1MAclaRp-3Q

I currently work as a Solutions Architect and we build "cool things" to help people improve their health.

With over 14 years of professional experience working as a Sr. Software Engineer specializing mainly on Web and Mobile apps using Microsoft technologies. My exploration into programming began at the age of 15;Turbo PASCAL, C, C++, JAVA, VB6, Action Scripts and a variety of other equally obscure acronyms, mainly as a hobby. After several detours, I am here today on the VB.NET to C# channel. I have worked on Web Apps + Client-side technologies + Mobile Apps + Micro-services + REST APIs + Event Communication + Databases + Cloud + Containers , which go together like coffee crumble ice cream.

I have been awarded Microsoft MVP each year since 2009, awarded C# Corner MVP for 2015, 2016,2017 and 2018, CodeProject MVP, MVA, MVE, Microsoft Influencer, Dzone MVB, Microsoft ASP.NET Site Hall of Famer with All-Star level and a regular contributor at various technical community websites such as CSharpCorner, CodeProject, ASP.NET and TechNet.

Books written:
" Book: Understanding Game Application Development with Xamarin.Forms and ASP.NET
" Book (Technical Reviewer): ASP.NET Core and Angular 2
" EBook: Dockerizing ASP.NET Core and Blazor Applications on Mac
" EBook: ASP.NET MVC 5- A Beginner's Guide
" EBook: ASP.NET GridView Control Pocket Guide

Comments and Discussions

 
-- There are no messages in this forum --