Thursday 27 August 2020

Migrate Asp.Net MVC 5 to .NET Core 3.1

.Net Core

 

I am working on a project with Asp.Net MVC 5 and we need to migrate this project to Asp.NET Core 3.1.

 I’ve found a few resources, blog posts, videos, and libraries that can help us with an upgrade to Asp.Net Core. In this post, I’m going to share them with you. So We used the below tool to check portability


.Net Portability Analyzer

.Net Portability Analyzer Tool helps us to analyze our solution to see how portable it is.

 

So the first step is to install the analyzer for visual studio. This extension can be found in visual studio marketplace

 

Go to Visual Studioà Tools à Options



Right-click on solution explorer and click on Analyze Project portability

 


 

It will build an HTML report like below to check compatibility

 

 

Asp.net Core Migration Step By Step

 

Step 1:-  Create a new empty ASP.NET Core web app with the same name as the previous project. So namespace will be match.

 

Step 2 :- Install the Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.StaticFiles NuGet packages. The ASP.NET runtime is modular, and you must explicitly opt in to serve static files

 

Step 3 :- Open the .csproj file and add a PrepareForPublish target: For example

  <Exec Command="bower install" />

</Target>

 

 

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

namespace WebApp1

{

    public class Startup

    {

        public void ConfigureServices(IServiceCollection services)

        {

            services.AddMvc();

        }

 

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

        {

            loggerFactory.AddConsole();

 

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

 

            app.UseStaticFiles();

 

            app.UseMvc(routes =>

            {

                routes.MapRoute(

                    name: "default",

                    template: "{controller=Home}/{action=Index}/{id?}");

            });

        }

    }

}

Step 5:-  Add a Controllers folder. Then add MVC controller class with the name HomeController.cs to the Controllers folder.

·       Add a Views folder.

·       Add a Views/Home folder.

·       Add an Index.cshtml, MVC view page to the Views/Home folder.

 

 

Test Your Running Application

Replace the contents of the Views/Home/Index.cshtml file with the following:

<h1>Hello world!</h1>

 

 

 

Step 6:-  Migrating functionality from the ASP.NET MVC project. We will need to move the following:

·       client-side content (CSS, fonts, and scripts)

·       controllers

·       views

·       models

·       bundling

·       filters

·       Log in/out, identity

 

 

Step 7:- Copy each of the methods from the ASP.NET MVC OldController to NewController

 

Step 8:- Copy the About.cshtmlContact.cshtml, and Index.cshtml Razor view files from the ASP.NET MVC project to the ASP.NET Core project.

 

Step 9:- Run the ASP.NET Core app and test each method.


Step 10:- For static content Add a Bower configuration file named bower.json to the project root (Right-click on the project, and then Add > New Item > Bower Configuration File). Add Bootstrap and jQuery to the file

 

Step 11:- Copy the favicon.ico file from the old MVC project to the wwwroot folder in the ASP.NET Core project.

 

Step 12:- Copy the _ViewStart.cshtml file from the old ASP.NET MVC project's Views folder into the ASP.NET Core project's Views folder. The _ViewStart.cshtml file has not changed in ASP.NET Core MVC.

  

Step 13:- Create a Views/Shared folder.

 

Step 14:- 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.

 

 Step 15:- Change some old features on razor view with news like followings

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

·       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.

 

Happy programming!!
Don’t forget to leave your feedback and comments below!
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
------------------------------------------------------------------------------

 

3 comments:

  1. I really enjoyed reading your blog. I really appreciate your information which you shared with us. If any one who want to create his/her carrier in dot net. Get a free demo call on 9311002620 or visit https://htsindia.com/Courses/microsoft-courses/dotnettraininginstituteinsouthdelhi

    ReplyDelete