The migration of ASP. NET MVC application to ASP and its similarities and differences

Source: Internet
Author: User
Tags dotnet

ASP. NET core is a new development framework that supports cross-platform, high-performance, open-source, which is more suitable for modern applications such as cross-platform, dorker support, integrated modern front-end development framework (such as NPM, Bower) than the original ASP. , gulp, etc.). In addition, it has better performance than ASP, and built-in features such as dependency injection are optimized for the development approach. But there are many similarities or similarities between them, such as using C # for development, providing MVC, Entity Framework, identity and other components to quickly build applications.

This article introduces the similarities and differences between ASP and ASP, by migrating a simple ASP. NET application, the main contents are:
.net core & ASP. Net Core &.
ASP. NET application Migration Analysis
Class Library Project Migration
0 completing a class library migration by creating a new project
0 Migration EntityFramework
Migrating ASP. NET MVC
0 creating a new ASP. NET Core MVC Project
0 routing Migrations
0 Filter Migration
0 Dependency Injection Migration
0Web Static resources and view migrations
Summary

Note: The VS2017 used in this article is version 15.6.4, and the. Net Core SDK version is 2.1.103.

. NET Core & ASP &. NET Standard

Before you explain how to migrate your ASP. Net core and ASP. Core,.net Core is a new development platform, the latest runtimes, SDKs, class libraries, tools (compilation tools, project template generation tools, and so on), and ASP. Core is a Web application development framework based on. NET core, and their relationship is actually the same as the. NET Framework and ASP.
But in use. NET development, a new noun. NET standard is present, and. NET Standard is one that can be used for all. NET program, in other words, if it is used. NET developed API (class library) conforms to. NET Standard, then the class library will be available for all. NET applications. If you developed a class library that was developed in the. NET Standard2.0 standard, as shown in the following table, the class library will be applied to. NET Core2.0,. NET Framework4.6.1 (requires SDK support for. NET Core2.0), Mono5.4, On the apps of Xamarin, UWP, and other platforms.

  

For more information about. Net Standard, refer to the documentation: Https://docs.microsoft.com/en-us/dotnet/standard/net-standard

Asp. NET application Migration Analysis

While. NET core is a new development platform (new runtime, new SDK, new Class Library, new tool), it is based on the. NET Framework, so it is still referenced and compatible with the. NET Framework for some APIs, so the. NET is migrated The core application allows you to analyze the code first.
1. Use the tool to analyze the code:
There is a tool called. NET Portability Analyzer dedicated to parsing code and referencing the assembly's support on different platforms in. NET, which can be searched and installed in the expansion management of VS, or on the page download vs plugin double-click Install (https:// Marketplace.visualstudio.com/items?itemname=connieyau.netportabilityanalyzer):

  

After installation, right-click the solution or project in the VS solution window to see two menu items, respectively, to analyze the portability of the Assembly and the configuration of the Migration Analyzer:

  

The configuration of the Migration Analyzer is primarily to configure the target platform and report output type:

  

is the Excel report (partial) for portability analysis of my blog app:
Summary of Portability:

  

Question Details:

  

Missing assemblies:

  

  Note: No more explanations for missing assemblies, unsure whether they are unresolved or not, this article briefly interprets these assemblies as supporting only the. Net Framework platform.
As you can see from the analysis report, other class library projects are 100% compliant with the. NET core or. NET Standard standards, in addition to the MVC project.
2, according to the actual situation of the project reference analysis:
Tool-to-code analysis is only a reference, after all, the use of the. Net Framework development of applications may refer to a number of third-party class libraries, these libraries may not support other platforms, some class libraries may support, but for the original version has a lot of break changes, the use of the method is inconsistent with the original, Therefore, the project reference is analyzed before the code migration, and it is technically feasible to analyze the code migration.
The implementation of warehousing in this example relies on components such as the entity Framework, EF MySQL, AUTOFAC, identity, and so on, which are also implemented under. Net core, although the API is subject to change, but technically there is no problem to migrate. The next step is to get started on how to complete the migration.

Class Library Project Migration

The. NET core project now, like the. NET Framework project, uses the MSBuild-formatted csproj file to manage the related properties of the project, such as name, version number, package dependencies, project dependencies, and so on. So the biggest change in the project's migration is to change the csproj content to. NET core (. NET core's csproj file details Reference document: Https://docs.microsoft.com/en-us/dotnet/core/ Tools/csproj), there are two ways to change the csproj file, if you are familiar with the file format, then change the original file directly, if unfamiliar then you can re-create the project by the way, let vs to complete the creation/modification work.

Complete the class library migration by creating a new project

This article will complete the project migration by re-creating the project to ensure the correctness of the csproj configuration file.
First, you create a. Net Standard class Library project with VS:

  

Then copy the corresponding code into the new project directory and delete the nonexistent namespace.
  NOTE:. NET core automatically recognizes the code files in the project directory, and the original. NET FX project needs to be introduced in the Csporj file, so it is possible to directly copy the code files to identify the project.

Migrating entityframework

In this case, the class library that implements warehousing relies on EF, and the class library that implements MySQL EF also relies on the EF-related components of my SQL.
In addition, with the launch of. Net Core, EF also launched the Efcore, the other new version. NET introduced in the options mode to configure the program, about the options mode can refer to the document: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/ configuration/options?view=aspnetcore-2.1
English Document: Https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration
So when migrating EF, in addition to creating a new project, you need to install a new NuGet package and modify the appropriate configuration code:
1. Install the Efcore components:
Install Microsoft.entityframeworkcore:

  

2. Handle the configuration of the Efcore:
Efcore Configuration Document: Https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/configuring-dbcontext

  

  Note: Efcore configuration information for different databases is done through the Dbcontextoptions object, so the construction method is no longer a simple connection string, and Efcore provides a dependency injection method for the ASP. To take advantage of dependency injection, the DbContext configuration can be assigned to an ASP. NET core project, including support for different data sources.

The Class library project in the example can be migrated by the above method, and then the MVC project is migrated.

Migrating ASP. NET MVC

  ASP. NET core is a change, in addition to cross-platform, it also changed the original ASP. NET ideas and development methods, such as MVC and API controller merge, embracing modern front-end development methods, dorcker support, etc. The next step is to understand the similarities and differences between ASP.

Create a new ASP. NET Core MVC Project

Compared to the class libraries authoring, the migration of MVC projects is much more complex, because the new ASP. NET is different in terms of project file structure, program initiation, and development methods, and the migration of ASP. NET MVC applications is still done in the same way that new projects are created:
Create an ASP. NET Core MVC application:

  

Here is the directory structure for the new project:

  

The same core of ASP. NET core MVC is still controllers, Models, views three directories, and the HTML resources are consolidated by Wwwroot directory, the configuration file is changed to JSON format, Removed the global.asax instead of the Program.cs and Startup.cs files.
Asp. NET-Developed Web application requires IIS to run as a host, and ASP. As a cross-platform Web application Development framework, its application must be completely decoupled from IIS, so it starts completely differently.
The program type is introduced in ASP. NET core, like the console app, which runs programs through the main method, while the main method of the ASP. NET core is to indicate that a startup type completes the creation of the Web server. While the startup type is responsible for the configuration of the entire application and the task of the HTTP request pipeline , the first thing to do is to migrate the contents of the original global and Owin startup files to the startup file. The following is a detailed introduction to the changes and migration methods of ASP.
MVC, in addition to the controller, model, view and other files, but also the need for routing, filters and other functional configuration, so first need to the original project related files (Controller, model, view, etc. note: Because the original project and ASP. Core MVC relies on identity to implement user management functions, so the part that involves user management does not need to be copied into the new project, modify the namespace reference such as:system.web.mvc-> MICROSOFT.ASPNETCORE.MVC and fix other namespace reference errors.
The next few other important aspects of migration are completed.

Routing migration

Transfer the routing configuration of the original routing configuration containing area to the Configure method of the Startup type:

  

  Note: This example has the name of the controller, but the ASP. MVC does not provide a way to differentiate controller names in the same manner as in ASP., this example solves the problem by modifying the name, such as requiring a namespace-sensitive controller to refer to:https://stackoverflow.com/ Questions/34306891/restrict-route-to-controller-namespace-in-asp-net-core

Filter migration

The configuration of the global filter is configured for MVC by the original Configureservices method that was registered to startup through the Filterconfig object:

  

  

Dependency Injection Migration

The ASP. NET core has built-in dependency injection capabilities, and even some components provide a dedicated outreach approach to adding their services to containers such as Efcore, Identity, and MVC.

  

The default for ASP. NET core is to add the DbContext, identity and MVC related services to the container code, in addition to conform to the idea of dependency injection, here the original code of warehousing and service types are registered in the container, its dependency through the construction method to inject.
Warehousing Code:

  

Business code:

  

Controller code:

  

Service Registration Code:

  

 Note: The implementation in this example is dependent on implementation rather than abstraction, which does not conform to the dependency inversion principle, so this is only used as an example of an ASP. NET core Dependency injection. In addition, in order for Efcore to support MySQL, you need to add the POMELO.ENTITYFRAMEWORKCORE.MYSQL component to your project:

  

Web static resources and view migrations

Asp. NET page is based on the HTML implementation, an ASP page in addition to the HTML code will also refer to some external static resources, such as CSS, JS, image, font and other content, and net in a dedicated bundle technology to manage these resources (for reference: " Asp. NET no magic--asp.net MVC interface Landscaping and using bundles to complete static resource management), then how does the ASP manage these resources?
The first thing to do is to deal with the access problem of static files in ASP. ASP. Because it relies on IIS, and static file access is already handled by IIS, there is no need to care in the program, but the ASP. NET core is different and needs to add the appropriate processing middleware in its request pipeline:

  

Note: The ASP. NET Core project template adds the middleware by default.
The management of the static resource, the ASP. NET core is configured with the Bundleconfig.json configuration file instead of the original bundleconfig type. Copy the original resource file to the Wwwroot directory of the ASP. NET core and configure it under the Bundleconfig.json file, as in (partial):

You will also need to add a Buildbundlerminifier component to your project after the configuration is complete:

  

compiling the project after introduction of buildbundlerminifier triggers resource binding and minimization operations:

  

  Note: Unlike ASP., the static resource management inside the core is processed during the compilation process, and through a JSON configuration file to complete the processing definition, in addition to Buildbundlerminifier can also use tools such as gulp to complete the optimization of resources, about the use of Gulp can be consulted: https://docs.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-2.0 &tabs=visual-studio%2caspnetcore2x#consume-bundleconfigjson-from-gulp

After the static resource migration is complete, the resource reference in view needs to be modified because of the change in the bundle mechanism (the @scripts and @styles types are no longer available in ASP. NET core to render styles and script references):

  

  

Operation Result:

  

Summary

The. NET core is a new development framework, and the 2.0 version of. NET core is relatively well-developed, and this article describes a simple example of the migration of ASP. NET MVC to. Even improved on the basis of the original. In addition,. NET core as a new framework it is open source and its documentation is more comprehensive relative to the. NET Framework (Chinese documents are relatively backward), so. NET core is easier to learn and use.

For migration, the first is to see the need and purpose, why migrate? For the sake of container? Cross-platform deployment? Migrating a project in a real project is very complex and risky, so if you need to consider the migration carefully, consider whether the migration is the best solution.

  

Reference:

Http://crbtech.in/Dot-Net-Training/9-things-know-converting-asp-dot-net-to-dot-net-core/
Https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1
https://docs.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-2.1& Tabs=visual-studio%2caspnetcore2x
Https://stackoverflow.com/questions/34306891/restrict-route-to-controller-namespace-in-asp-net-core

https://docs.microsoft.com/en-us/aspnet/core/migration/proper-to-2x/?view=aspnetcore-2.1

This article link: http://www.cnblogs.com/selimsong/p/8870286.html

The migration of ASP. NET MVC application to ASP and its similarities and differences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.