Migrate from ASP.NET to ASP.NET Core

1. Create the ASP.NET Core project

Create a new empty ASP.NET Core web app with the same name as the previous project so the namespaces in the two projects match. Having the same namespace makes it easier to copy code between the two projects. You'll have to create this project in a different directory than the previous project to use the same name.

2. Configure the site to use MVC

When targeting .NET Core, the Microsoft.AspNetCore.App metapackage is referenced by default. This package contains packages commonly used packages by MVC apps.

Open the Startup.cs file and change the code to match the following:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // ...

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

3. Add controllers and views

  • Copy each of the methods from the ASP.NET MVC HomeController to the new HomeController.

  • Copy the About.cshtml, Contact.cshtml, and Index.cshtml Razor view files from the ASP.NET MVC project to the ASP.NET Core project.

4. Copy static content

In previous versions of ASP.NET MVC, static content was hosted from the root of the web project and was intermixed with server-side files. In ASP.NET Core, static content is hosted in the wwwroot folder.

  • Copy the static content from your old ASP.NET MVC app to the wwwroot folder in your ASP.NET Core project.

  • Copy the favicon.ico file from the old MVC project to the wwwroot folder in the ASP.NET Core project.

5. Migrate the layout file

  • Copy the _ViewStart.cshtml file from the old ASP.NET MVC project's Views folder into the ASP.NET Core project's Views folder.

  • [OPTIONAL] Copy _ViewImports.cshtml from the FullAspNetCore MVC project's Views folder into the ASP.NET Core project's Views folder. Remove any namespace declaration in the _ViewImports.cshtml file.

  • Copy the _Layout.cshtml file from the old ASP.NET MVC project's Views/Shared folder into the ASP.NET Core project's Views/Shared folder.

Open _Layout.cshtml file and make the following changes (the completed code is shown below):

  • Replace @Styles.Render("~/Content/css") with a <link> element to load bootstrap.css (see below).

  • Remove @Scripts.Render("~/bundles/modernizr").

  • Comment out the @Html.Partial("_LoginPartial") line (surround the line with @*...*@).

  • Replace @Scripts.Render("~/bundles/jquery") with a <script> element.

  • Replace @Scripts.Render("~/bundles/bootstrap") with a <script> element.

6. Solve HTTP 500 errors

There are many problems that can cause a HTTP 500 error message that contain no information on the source of the problem, therefore it's good to configure developer exception page.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    
    // ...
}

Last updated