Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (151).NET Core  (25)Angular  (49)ASP.NET  (48)ASP.NET Core  (64)ASP.NET MVC  (86)Azure  (28)Blazor  (97)DocIO  (20)Essential JS 2  (71)Essential Studio  (163)File Formats  (20)Flutter  (111)JavaScript  (148)Microsoft  (101)PDF  (59)React  (33)Succinctly series  (122)Syncfusion  (480)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (26)Webinar  (25)Windows Forms  (55)WinUI  (21)WPF  (122)Xamarin  (130)XlsIO  (24)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (107)Chart  (51)Cloud  (9)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (5)DataGrid  (47)Development  (257)Doc  (7)DockingManager  (1)eBook  (93)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (20)Extensions  (13)File Manager  (4)Gantt  (9)Gauge  (7)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (227)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (23)Performance  (6)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (6)Road Map  (9)Scheduler  (26)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (19)Solution Services  (2)Spreadsheet  (9)SQL  (7)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (60)UI  (131)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (19)Visual Studio Code  (11)Web  (215)What's new  (140)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Easy Steps to Migrate an ASP.NET MVC Project to ASP.NET Core Project

Easy Steps to Migrate an ASP.NET MVC Project to an ASP.NET Core Project

ASP.NET MVC is a framework for developing web applications. It works on the model-view-controller pattern, and you can use it to develop only on Windows systems.

On the other hand, ASP.NET Core is a cross-platform web development framework that supports developing applications on Windows, Mac, Linux, iOS, and Android platforms. It has less maintenance, high performance, and more versatile features compared to the ASP.NET MVC.

In this blog post, we are going to learn how to upgrade an ASP.NET MVC web application by migrating it to ASP.NET Core by following these simple steps:

  1. Create an ASP.NET MVC sample project.
  2. Create an ASP.NET Core sample project.
  3. Migrate a register and login page to ASP.NET Core.
  4. Migrate views and controllers to ASP.NET Core.
  5. Migrate all layout files to ASP.NET Core.
  6. Test each action method in the Home controller.

Prerequisites

Create an ASP.NET MVC sample project

First, we are going to create a simple ASP.NET MVC project by following these steps:

  1. Open Visual Studio and open the File menu. Then, navigate to New -> Project.
  2. Now, select ASP.NET Web Application (.NET Framework) and click Next.
  3. Name the project MVCWebApplication1, so that the namespace matches the ASP.NET Core project that will be created later in this blog post. Now, click Create.
  4. Next, select the MVC option, and then click Create.
  5. Finally, run the application.
    Create a Simple ASP.NET MVC Project

We have created a simple ASP.NET MVC project.

Create an ASP.NET Core sample project

Now, we are going to create a simple ASP.NET Core project.

  1. In Visual Studio, open the File menu and then navigate to New -> Project.
  2. Now, select the ASP.NET Core Web Application option and click Next.
  3. Then, the Configure your new project dialog will open. In it, enter the project name CoreWebApplication1. Using the same namespace makes it easier to copy code between the two projects. Now, click Create.
  4. In the Create a new ASP.NET Core Web Application dialog, confirm that the .NET Core and ASP.NET Core 3.1 options are selected. Then, select the Web Application (Model-View-Controller) project template and click Create.
  5. Finally, run the application.
    Creating a Simple ASP.NET Core Project

We have created a basic ASP.NET Core project.

Migrate register and login page to ASP.NET Core

In this section, we are going to migrate a register and login page to ASP.NET Core. Let’s get started.

  1. For authentication and identity features, install the following NuGet packages in ASP.NET Core:
  2. In the Startup.cs file, add the Startup.ConfigureService method to use the Entity Framework and Identity services as shown in the following code:
    public void ConfigureServices(IServiceCollection services)
    {
        // Add EF services to the services container.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
    
         services.AddMvc();
    }
  3. Create a Models folder in the ASP.NET Core project and add the two classes ApplicationUser and ApplicationDBContext to their corresponding references from the ASP.NET MVC project. You can find these classes together in the Models/IdentityModels.cs file path in the ASP.NET MVC project.
    Refer to the following screenshot.
    Add the two classes ApplicationUser and ApplicationDBContext to their corresponding references from the ASP.NET MVC projectAlso, you can create separate classes for these classes as shown in the following code samples:
    ApplicationUser.cs:

    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    
    namespace NewMvcProject.Models
    {
      public class ApplicationUser : IdentityUser
      {
      }
    }

    ApplicationDBContext.cs:

    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.Data.Entity;
    
    namespace NewMvcProject.Models
    {
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
    
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
                // Customize the ASP.NET Core Identity model and override the defaults if needed.
                // For example, you can rename the ASP.NET Core Identity table names and more.
                // Add your customizations after calling base.OnModelCreating(builder);
            }
        }
    }
  4. Add the following namespaces in the Startup.cs file to compile:
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
  5. Add a new Razor view called _LoginPortal to the Views -> Shared folder and include the following code:
    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    
    @if (SignInManager.IsSignedIn(User))
    {
        <form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
                </li>
                <li>
                    <button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
                </li>
            </ul>
        </form>
    }
    else
    {
        <ul class="nav navbar-nav navbar-right">
            <li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
            <li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
        </ul>
    }

Migrate views and controllers to ASP.NET Core

Let’s migrate the views and controllers to the ASP.NET Core project with these simple steps:

  1. Navigate to Views -> Home directory. Then, add the About.cshtml, Contact.cshtml, and Index.cshtml view files from the ASP.NET MVC project.
  2. Then, in the Controllers -> HomeController.cs file, add all the methods from the ASP.NET MVC project.

Migrate the layout files to ASP.NET Core

Now, we are going to migrate all the layout files from the ASP.NET MVC project to the ASP.NET Core project.

  1. In the Views folder, add the _ViewStart.cshtml file from the ASP.NET MVC project.
  2. Then, in the Views -> Shared folder, add the _Layout.cshtml file from the ASP.NET MVC project.
  3. Make the following changes in the _Layout.cshtml file:
    • Replace the @Styles.Render(“~/Content/css”) code with the following:
      <link rel="stylesheet"
              href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
              crossorigin="anonymous">
    • Remove the @Scripts.Render(“~/bundles/modernizr”) code.
    • Then, replace the @Scripts.Render(“~/bundles/jquery”) and @Scripts.Render(“~/bundles/bootstrap”) code with the following:
      <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
             <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
             integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

Test each action method in the Home controller

Now, run the ASP.NET Core application.

Run the ASP.NET Core ProjectThen, navigate to the Home, About, Contact, Login, and Register tabs to ensure the changes are correct. Refer to the following screenshots.

About:

Check the About tabContact:

Check the Contact tab

Conclusion

Thanks for reading! In this blog post, we have learned how to migrate an ASP.NET MVC project to an ASP.NET Core project. By doing so, you can create cross-platform apps for Windows, Mac, Linux, iOS, and Android platforms. So, try out the process yourself and leave your feedback in the comments section of this blog post!

With over 70 components, our Syncfusion ASP.NET Core toolkit powered by Essential JS 2 contains all you need for building line-of-business applications. It includes popular widgets such as a DataGrid, Charts, Gantt Chart, Diagram, Spreadsheet, Scheduler, and Pivot Table. Use them to enhance your productivity!

For existing customers, the new version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the available features. Also, we encourage you to try our samples on GitHub.

Also, you can contact us through our support forumDirect-Trac, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top