[ad_1]
Microsoft’s ASP.Internet Main 6, which has been out there for creation use because November 8, introduces a simplified web hosting model that minimizes the boilerplate code that you would otherwise want to compose to get your ASP.Web Main software up and managing. ASP.Web Core 6 makes a little bit a lot easier to create a new web application from scratch, compared with ASP.Net Main 5.
But what if you want to update an ASP.Web Core 5 project to ASP.Internet Main 6? In that scenario, you ought to be conscious of the code you will have to have to create to migrate ASP.Web Core 5 code to ASP.Internet Main 6. This article presents numerous code samples that display how you can do this.
To function with the code examples delivered in this post, you should really have Visual Studio 2022 mounted in your system. If you do not presently have a copy, you can download Visible Studio 2022 here.
Build an ASP.Web Main Internet API venture in Visual Studio 2022
First off, let us build an ASP.Web Main project in Visible Studio 2022. Next these steps will create a new ASP.Internet Main World-wide-web API 6 venture in Visual Studio 2022:
- Launch the Visual Studio 2022 IDE.
- Click on on “Create new venture.”
- In the “Create new project” window, pick “ASP.Internet Main Website API” from the checklist of templates displayed.
- Simply click Up coming.
- In the “Configure your new project” window, specify the title and spot for the new task.
- Optionally check out the “Place remedy and venture in the very same directory” verify box, based on your choices.
- Click on Subsequent.
- In the “Additional Information” window proven future, ensure that the examine box that suggests “Use controllers…” is checked, as we’ll be working with controllers as an alternative of small APIs in this example. Leave the “Authentication Type” established to “None” (default).
- Guarantee that the check out containers “Enable Docker,” “Configure for HTTPS,” and “Enable Open up API Support” are unchecked as we will not be utilizing any of all those functions here.
- Click on Make.
We’ll use this ASP.Web Main 6 Web API job to illustrate migrations of ASP.Internet Core 5 code to ASP.Net Core 6 in the subsequent sections of this write-up.
The Plan course in ASP.Web Core 5
The following code snippet illustrates what a typical System class appears to be like like in ASP.Net Core 5.
public course Plan
public static void Main(string[] args)
CreateHostBuilder(args).Establish().Operate()
general public static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args).
ConfigureWebHostDefaults(x => x.UseStartup())
The Application class in ASP.Web Main 6
With the introduction of the simplified web hosting product in ASP.Net Main 6, you no for a longer period have to use the Startup class. You can read through a lot more about this in my earlier report below. Here’s how you would write a common Software course in ASP.Internet Main 6:
var builder = WebApplication.CreateBuilder(args)
// Insert expert services to the container
builder.Companies.AddControllers()
var app = builder.Make()
// Configure the HTTP ask for pipeline
app.UseAuthorization()
app.MapControllers()
app.Run()
Add middleware in ASP.Web Main 5
The following code snippet exhibits how you can increase a middleware element in ASP.Net Main 5. In our illustration, we’ll increase the reaction compression middleware.
community class Startup
public void Configure(IApplicationBuilder application)
application.UseResponseCompression()
Increase middleware in ASP.Internet Main 6
To include a middleware ingredient in ASP.Net Core 6, you can use the subsequent code.
var builder = WebApplication.CreateBuilder(args)
var application = builder.Establish()
application.UseResponseCompression()
app.Run()
Add routing in ASP.Net Core 5
To include an endpoint in ASP.Internet Main 5, you can use the pursuing code.
general public course Startup
general public void Configure(IApplicationBuilder application)
application.UseRouting()
app.UseEndpoints(endpoints =>
endpoints.MapGet("/exam", () => "This is a examination information.")
)
Incorporate routing in ASP.Internet Main 6
You can incorporate an endpoint in ASP.Web Core 6 utilizing the subsequent code.
var builder = WebApplication.CreateBuilder(args)
var application = builder.Build()
app.MapGet("/take a look at", () => "This is a exam concept.")
app.Run()
Notice that in ASP.Net Core 6 you can increase endpoints to WebApplication devoid of obtaining to make specific phone calls to the UseRouting or UseEndpoints extension methods.
Increase companies in ASP.Internet Main 5
The following code snippet illustrates how you can include services to the container in ASP.Internet Core 5.
public class Startup
community void ConfigureServices(IServiceCollection companies)
// Incorporate designed-in providers
services.AddMemoryCache()
companies.AddRazorPages()
products and services.AddControllersWithViews()
// Insert a custom services
expert services.AddScoped()
Include services in ASP.Net Main 6
To add providers to the container in ASP.Net Core 6, you can use the next code.
var builder = WebApplication.CreateBuilder(args)
// Add built-in solutions
builder.Providers.AddMemoryCache()
builder.Solutions.AddRazorPages()
builder.Solutions.AddControllersWithViews()
// Add a personalized support
builder.Solutions.AddScoped()
var app = builder.Construct()
Examination an ASP.Net Core 5 or ASP.Internet Core 6 application
You can check an ASP.Web Core 5 application working with both TestServer or WebApplicationFactory. To exam working with TestServer in ASP.Web Main 5, you can use the following code snippet.
[Fact]
community async Activity GetProductsTest()
applying var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder =>
builder.UseTestServer()
.UseStartup()
)
.ConfigureServices(products and services =>
solutions.AddSingleton()
)
.Build()
await host.StartAsync()
var consumer = host.GetTestClient()
var response = await client.GetStringAsync("/getproducts")
Assert.Equivalent(HttpStatusCode.Okay, response.StatusCode)
The adhering to code snippet displays how you can check your ASP.Net Main 5 software using WebApplicationFactory.
[Fact]
community async Activity GetProductsTest()
var software = new WebApplicationFactory()
.WithWebHostBuilder(builder =>
builder.ConfigureServices(companies =>
products and services.AddSingleton()
)
)
var shopper = application.CreateClient()
var reaction = await consumer.GetStringAsync("/getproducts")
Assert.Equivalent(HttpStatusCode.Alright, reaction.StatusCode)
You can use the same code to check making use of TestServer or WebApplicationFactory in .Web 5 and .Net 6.
Insert a logging provider in ASP.Web Main 5
Logging providers in ASP.Web Core are applied to retailer logs. The default logging companies integrated in ASP.Net Main are the Debug, Console, EventLog, and EventSource logging suppliers.
You can use the ClearProviders technique to very clear all logging providers and incorporate a particular logging supplier or your individual personalized logging provider. The following code snippet illustrates how you can take out all ILoggerProvider scenarios and incorporate the Console logging provider in ASP.Internet Core 5.
community static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
logging.ClearProviders()
logging.AddConsole()
)
.ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup()
)
Incorporate a logging supplier in ASP.Web Core 6
In ASP.Web Main 6, when you call WebApplication.CreateBuilder, it provides the Console, Debug, EventLog, and EventSource logging suppliers. The subsequent code snippet demonstrates how you can very clear the default logging vendors and insert only the Console logging provider in ASP.Net Main 6.
var builder = WebApplication.CreateBuilder(args)
//Obvious default logging providers
builder.Logging.ClearProviders()
//Code to incorporate expert services to the container
builder.Logging.AddConsole()
var application = builder.Construct()
The code examples presented in this article illustrate the various techniques we insert middleware, routing, expert services, and logging suppliers in ASP.Net Main 5 and in ASP.Web Main 6, as properly as variations in the System class and screening. These snippets must help you when doing the job with ASP.Net Core 6 purposes, and get you off to a good start off when you migrate your ASP.Web Main 5 applications to ASP.Web Main 6.
Copyright © 2022 IDG Communications, Inc.
[ad_2]
Supply backlink
More Stories
Honor Magic Vs hands-on impressions: The on-paper foldable king
Has the FTX mess iced venture interest in crypto? • TechCrunch
Total Points from Google Classroom