ASP.NET and ASP.NET Core
About the Author: Manish Luhana
Microsoft business applications specialist and certified trainer.
Categories: .NET0 CommentsPublished On: 22 March 2022

There are even more incentives to use ASP.NET Core now that.NET 6 has been released. However, moving old code to ASP.NET Core might appear to be a major expenditure. Today, we’ll show you how to make the migration to ASP.NET Core go smoother. There are some simple modifications you can make today to make the transition to ASP.NET Core simpler tomorrow.

Let’s start with a hypothetical situation. Let’s look at how to update code in a 10-year-old programme to make it compatible with ASP.NET Core. The ShoppingCartController.cs from the MVC Music Store project that was used to demonstrate ASP.NET MVC3 will be migrated in the following parts.

Controllers can always be shared.

Controllers are the first item you may share between the two projects. Many groups want the new website to function in the same way as the old one. And we mean “the same” when we say “the same.” If you solve an issue in one project, the identical solution must be applied to both sites. Sharing the same file in different projects is one of the simplest ways to ensure this functionality. Fortunately, ASP.NET Core makes advantage of the new SDK project files. Because the files are fairly readable, it’s simple to access the csproj file and make modifications.

To begin sharing a Controller class, build an ItemGroup> and include a reference to the existing class in it. Here’s an example of how to share the ShoppingCartController.cs by modifying the ASP.NET Core project’s csproj file.

<ItemGroup> <Compile Include=”..MvcMusicStoreControllersShoppingCartController.cs”

LinkBase=”Controllers” /> </ItemGroup>

Okay, the file is now included in the project, but as you may expect, the ASP.NET Core project no longer compiles. The Controller class in ASP.NET Core utilises Microsoft.AspNetCore.Mvc instead of System.Web.Mvc.

Here’s an example of how the ShoppingCartController.cs may fix the compiler problem by using both namespaces.

#if NET using Microsoft.AspNetCore.Mvc; #else using System.Web.Mvc; #endif

There are a few more locations in the ShoppingCartController that need to be modified, but the procedure is the same. We can make the class flexible enough to compile for both projects by using C# preprocessing directives.

YOU MAY WISH TO DEVELOP IMPLEMENTATION SPECIFIC FILES FOR SCENARIOS WITH SIGNIFICANT CHUNKS OF CODE THAT FUNCTION DIFFERENTLY FOR ASP.NET CORE. CREATE A PARTIAL CLASS AND EXTRACT THOSE CODE BLOCKS TO NEW METHOD(S) THAT DIFFER BETWEEN THE TWO WEB APP TARGETS, THEN USE CSPROJ TO CONTROL WHICH FILES ARE INCLUDED WHEN CREATING THE PROJECT.

Models can be shared.

We’ll want to share the Models they return now that we can share Controllers. In many cases, this will simply start functioning after we include them in the csproj file by adding another <ItemGroup>. However, if your models also utilize System. Web, we may apply the same method as we did for Controllers. To begin, change the namespaces so that the same class file may be used in both projects. To add ASP.NET Core support, keep utilising the C# precompiler directives.

Here’s an example of how you may change the [Bind] property.

#if !NET [Bind(Exclude=”OrderId”)] #endif public partial class Order { [ScaffoldColumn(false)] #if NET

[BindNever] #endif publicintOrderId{ get; set; } … …

Views can be shared.

We can even discuss our points of view. We can use the similar way to share files like _Layout.cshtml by editing the csproj file. You may also use C# precompiler directives inside the view to make the file flexible enough to be utilised by both projects.

Here’s how a master page with mixed support for ASP.NET Child Actions and ASP.NET Core View Components appears to render the section of the page that knows how many things are in the shopping cart looks.

@{ #if NET <text>@awaitComponent.InvokeAsync(“CartSummary”)</text> #else @Html.RenderAction(“CartSummary”, “ShoppingCart”); #endif }

Bringing everything to a conclusion

Static data like as CSS, JavaScript, and image are also included in the ability to exchange code. You can add flexibility to your web app today, step by step, to make the transition to ASP.NET Core easier.

You can find a complete tutorial with samples at MvcMusicStoreMigration if you want more extensive instructions on how to migrate the whole ShoppingCartController.cs file. The tour will also show you how to migrate your web project one controller at a time by running both ASP.NET and ASP.NET Core from the same IIS Application Pool.

We’ll give you a few more pointers if you’re ready to start working on your ASP.NET Core conversion.

  • To enable netstandard, you’ll need to upgrade your NuGet packages.
  • To exchange code across ASP.NET and ASP.NET Core, change your class libraries to netstandard.
  • Find references to System. Web in your class libraries and replace them with new interfaces. You may quickly transition between ASP.NET and ASP.NET Core capabilities by using dependency injection.
tech mentor sidebanner
  • Continue reading
  • Continue reading
  • Continue reading
  • Continue reading
  • Continue reading

Leave A Comment