Convert site from ASP.NET MVC to ASP.NET Core MVC
In this guide we will show you how you can convert an old site from ASP.NET MVC to ASP.NET Core MVC.
This guide assumes some familiarity with ASP.NET Core MVC.
This document is primarily based on Microsoft's guide to migrating from ASP.NET MVC to ASP.NET Core MVC.
Create a new ASP.NET Core Web Application
In our experience, the easiest way of converting a site to .Net Core is to create a new solution, and then copy over all the relevant bits and pieces from the old solution. This way we know exactly what's been brought over.
- Create a new ASP.NET Core Web Application that has the same namespace as your source project. This will make transferring the code files go a lot smoother.
- Delete the automatically generated files in
Controllers/ folders, if any.
- Install the Relatude.Core nuget package.
- Copy the Content.definition file from the source project root to the target project root.
Go to the file's properties, and set 'Copy to output directory' to always.
Open and save the Content.definition.
(If you get an error, you probably have an old version of the Relatude Ontology Visual Studio extension installed. Uninstall the old one, quit VS, then install the newest version with the Relatude tool. The tool can be downloaded from the frontpage.)
- If you have a database in your source's 'App_Data'-folder, copy it to #target root#/WAF.Data/Content/Database
- At this point you should update your appsettings.json. It should look something like this:
- Now either copy over, or create controllers that match the ones in the source project.
- Copy over the view files from the source project.
- Copy over any other code files used.
- Update the usings and return values in all the code files and views. If any of the views are using ViewHelper-methods then these will have to be converted to ViewComponents or Partials or whatever you feel is most appropriate, since ViewHelper isn't supported in Core.
- Update all calls to WAFContext (
WAF.Presentation.Web.WAFContext) to instead use WAFNativeContext (WAF.API.Web.WAFNativeContext) injection.
In a controller this would look like this:
In a view, it would look like this:
- Copy over any static content. This should go in the wwwroot folder. You'll need to update all references to this content.
- If your old project uses a BundleConfig/BundleCollection for the static files, you can install the nuget package BundlerMinifier.Core, and create a bundleconfig.json in the target project root. If your old BundleConfig.cs file looks like this:
Then your bundleconfig.json could look like this:
And finally you'll need to update the references to your bundle(s).
- One of the last things to do is to update the Program.cs file with everything needed to get Relatude working.
An example file could look like this:
Note all the WAF-specific lines.
- Finally, for the moment, we need to add the following line to the (first)
<PropertyGroup>-element in the .csproj file: