This legacy MVC application is build with ASP.NET 4.7.2. Follow the steps below to port this application to .NET5.0.
- Portability analyzers
- try-convert
- Create wwwroot folder
- Migrate
packages.config
- Migrate
Global.asax
- Remove files
- Static files
- Configuration
App_Start
migration- MVC Controllers
- Migrate ASP.NET Web API to ASP.NET Core
- Sessions
- Dependency injection
- Logging
- EntityFramework
- Blazor
Optionally, you can use of the following portability analyzers before you start the upgrade. These tools will analyze assemblies and provides a detailed report about the impact of porting application to specified .NET platform(s).
For non-web application, you can use the try-convert
tool. This tool will try to migrate your .NET Framework project. Find more info here. Unfortunately, it doesn't work for web application like the eShopLegacyMVC project.
- Move Content to wwwroot/css/
- Move Scripts to wwwroot/scripts/
- Move Pics to wwwroot/pics/
- Move fonts to wwwroot/fonts/
- Move Images to wwwroot/images/
- Move favicon.ico to wwwroot
The migration tool doesn't support ASP.NET projects. However, there is a work around. Update the
.csproj
like is mentioned in the comment before continue.
- Migrate
packages.config
toPackageReference
. Follow the steps in this tutorial. Thepackages.config
will be removed automatically.
If it doesn't work for you find the upgraded
.csproj
[here](TODO: link to upgraded project).
The migrate tool is of course not a magically tool that will do all work. There will still be things you'll need to do manually. Remove the following legacy packages (either not compatible with NET5.0 or just legacy packages).
Remove packages from .csproj
Antlr
- All
Autofac.*
packages. We can use Microsoft's built-in dependency injection bootstrap
- All
jQuery.*
packages log4net
Microsoft.ApplicationInsights.Agent.Intercept
- All
Microsoft.AspNet.*
packages Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Microsoft.Net.Compilers
Microsoft.Web.Infrastructure
Modernizr
popper.js
response
WebGrease
Replace
EntityFramework
withMicrosoft.EntityFrameworkCore
version 5.0.2
Upgrade versions
System.Diagnostics.DiagnosticSource
andSystem.Diagnostics.PerformanceCounter
to 5.0.1System.Diagnostics.PerformanceCounter
to 5.0.0System.Buffers
to 4.5.1System.Runtime.CompilerServices.Unsafe
to 5.0.0
Install
Microsoft.EntityFrameworkCore.SqlServer
version 5.0.2Microsoft.EntityFrameworkCore.Design
version 5.0.2
Clean up .csproj
- Clean up the
.csproj
(remove everything except for the PackageReferences and the root) - Update the target framework and don't forget to update
<Project />
element More info [here] (https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-5.0&tabs=visual-studio#update-the-target-framework)
Find upgraded
.csproj
[here](TODO: link to upgraded project).
- Create
Program.cs
- Create
Startup.cs
- Make sure you enable MVC
Follow steps here
AssemblyInfo.cs
web.config
- if you are running .NET Core app in IIS you will need to keep web.config and migrate.
Global.asax
ApplicationInsights.config
App.config
Follow steps here.
Read about configurations here.
- Create
appsettings.json
manually or in VS chooseApp Settings File
inAdd new Item
dialog - Create
ConnectionStrings
withDefaultConnection
. Get connection string fromweb.config
- Move settings (
UseMockData
andUseCustomizationData
) toappsettings.json
Create parent elementDataSettings
- Learn how to work with options here
BundleConfig.cs
- Install
BuildBundlerMinifier
NuGet package - Create
bundleconfig.json
in root - Create one bundle for styles and one for scripts
- Follow tutorial here
- Remove
BundleConfig.cs
- Install
- Remove
FilterConfig.cs
- Remove
RouteConfig.cs
- Remove
WebApiConfig.cs
- Update script and style references in MVC views
- Update namespaces in controllers
HttpNotFound
=>NotFound
new HttpStatusCodeResult(HttpStatusCode.BadRequest);
=>BadRequest()
- Remove the
Include
property this.Request.Url.Scheme
tothis.Request.Scheme
- Inject
IWebHostEnvironment
in controller and use instead ofServer.MapPath
Learn about components here
- Change CatalogStatisticsController to a ViewComponent. Call component in razor view (
Views/Catalog/Index.cshtml
) - Change
Html.Partial
toHtml.PartialAsync
Read more here
services.AddSession()
call inStartup.cs
app.UseSession()
call inStartup.cs
- Use
Context.Session
instead ofHttpContext.Current.Session
- Move
ApplicationModule registrations
toStartup.cs
- Remove ApplicationModule class
Read more here
We will use Microsoft.Extensions.Logging
abstraction and install log4net
provider.
- Install
Microsoft.Extensions.Logging.Log4Net.AspNetCore
package - Follow steps here
- Update CatalogController and PicController and inject
ILogger<> logger
instead of usinglog4net
directly
Read more about Microsoft.Extensions.Logging
here
- Configure EntityFramework in
Startup.cs
- Update CatalogDBContext
- Change CatalogDBInitializer
- Remove
CreateDatabaseIfNotExists
- Make
Seed
public method and call inStartup.cs
using var scope = app.ApplicationServices.CreateScope(); using var context = scope.ServiceProvider.GetService<CatalogDBContext>(); context.Database.Migrate(); new CatalogDBInitializer(env, scope.ServiceProvider.GetService<DataSettings>()).Seed(context);
- Remove
- Update CatalogService
If all compile errors have been fixed add a migration to set up the database.
dotnet ef migrations add InitialCreate
- Verify if everything work as expected
- If you run the application and get a 404 make sure that there is default route configured
Follow the Blazor tutorial.
- TODO: more documentation here
- Use Blazor instead of MVC