[ad_1]
Microsoft’s ASP.Internet Core 6, which has been readily available for creation use due to the fact November 8, introduces a simplified internet hosting model that reduces the boilerplate code that you would if not require to create to get your ASP.Web Core software up and operating. ASP.Internet Main 6 makes a bit less difficult to create a new internet software from scratch, in comparison with ASP.Web Main 5.
But what if you want to update an ASP.Internet Main 5 challenge to ASP.Web Main 6? In that circumstance, you should be knowledgeable of the code you will need to write to migrate ASP.Web Core 5 code to ASP.Net Main 6. This posting presents numerous code samples that exhibit how you can do this.
To do the job with the code illustrations offered in this short article, you should really have Visual Studio 2022 mounted in your process. If you don’t by now have a copy, you can obtain Visual Studio 2022 listed here.
Produce an ASP.Internet Core Internet API job in Visible Studio 2022
Initial off, let us produce an ASP.Net Core job in Visible Studio 2022. Adhering to these steps will develop a new ASP.Internet Core Web API 6 undertaking in Visible Studio 2022:
- Launch the Visible Studio 2022 IDE.
- Click on on “Create new task.”
- In the “Create new project” window, decide on “ASP.Net Main World-wide-web API” from the record of templates displayed.
- Click Next.
- In the “Configure your new project” window, specify the name and site for the new undertaking.
- Optionally check out the “Place resolution and undertaking in the very same directory” look at box, depending on your tastes.
- Click Subsequent.
- In the “Additional Information” window demonstrated subsequent, make sure that the test box that says “Use controllers…” is checked, as we’ll be applying controllers instead of minimum APIs in this example. Leave the “Authentication Type” set to “None” (default).
- Guarantee that the examine boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open up API Support” are unchecked as we won’t be utilizing any of people features below.
- Click on Make.
We’ll use this ASP.Web Core 6 Website API job to illustrate migrations of ASP.Internet Core 5 code to ASP.Internet Main 6 in the subsequent sections of this short article.
The Plan class in ASP.Internet Main 5
The next code snippet illustrates what a typical Software class appears to be like like in ASP.Web Core 5.
public course Application
community static void Major(string[] args)
CreateHostBuilder(args).Construct().Operate()
public static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args).
ConfigureWebHostDefaults(x => x.UseStartup())
The Plan course in ASP.Net Core 6
With the introduction of the simplified internet hosting model in ASP.Net Main 6, you no extended have to use the Startup class. You can examine far more about this in my before report in this article. Here’s how you would publish a normal Application class in ASP.Web Main 6:
var builder = WebApplication.CreateBuilder(args)
// Add companies to the container
builder.Solutions.AddControllers()
var app = builder.Create()
// Configure the HTTP request pipeline
app.UseAuthorization()
application.MapControllers()
app.Run()
Increase middleware in ASP.Internet Core 5
The subsequent code snippet demonstrates how you can include a middleware ingredient in ASP.Internet Core 5. In our illustration, we’ll insert the response compression middleware.
community course Startup
public void Configure(IApplicationBuilder app)
app.UseResponseCompression()
Incorporate middleware in ASP.Net Main 6
To include a middleware ingredient in ASP.Internet Main 6, you can use the pursuing code.
var builder = WebApplication.CreateBuilder(args)
var app = builder.Develop()
application.UseResponseCompression()
app.Run()
Increase routing in ASP.Net Main 5
To add an endpoint in ASP.Web Main 5, you can use the adhering to code.
community class Startup
public void Configure(IApplicationBuilder app)
application.UseRouting()
application.UseEndpoints(endpoints =>
endpoints.MapGet("/take a look at", () => "This is a examination concept.")
)
Insert routing in ASP.Internet Main 6
You can incorporate an endpoint in ASP.Web Main 6 using the subsequent code.
var builder = WebApplication.CreateBuilder(args)
var app = builder.Establish()
app.MapGet("/exam", () => "This is a test message.")
application.Operate()
Observe that in ASP.Internet Main 6 you can increase endpoints to WebApplication without the need of acquiring to make express phone calls to the UseRouting or UseEndpoints extension techniques.
Add providers in ASP.Internet Main 5
The following code snippet illustrates how you can add solutions to the container in ASP.Web Main 5.
public course Startup
public void ConfigureServices(IServiceCollection expert services)
// Insert constructed-in products and services
solutions.AddMemoryCache()
companies.AddRazorPages()
services.AddControllersWithViews()
// Increase a tailor made provider
solutions.AddScoped()
Add solutions in ASP.Net Main 6
To add services to the container in ASP.Net Main 6, you can use the next code.
var builder = WebApplication.CreateBuilder(args)
// Include built-in products and services
builder.Companies.AddMemoryCache()
builder.Services.AddRazorPages()
builder.Solutions.AddControllersWithViews()
// Increase a tailor made support
builder.Services.AddScoped()
var app = builder.Build()
Examination an ASP.Internet Core 5 or ASP.Web Core 6 application
You can take a look at an ASP.Web Core 5 software working with either TestServer or WebApplicationFactory. To test using TestServer in ASP.Internet Core 5, you can use the following code snippet.
[Fact]
general public async Process GetProductsTest()
working with var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder =>
builder.UseTestServer()
.UseStartup()
)
.ConfigureServices(services =>
solutions.AddSingleton()
)
.Make()
await host.StartAsync()
var consumer = host.GetTestClient()
var response = await client.GetStringAsync("/getproducts")
Assert.Equal(HttpStatusCode.Alright, response.StatusCode)
The pursuing code snippet shows how you can examination your ASP.Net Core 5 software employing WebApplicationFactory.
[Fact]
community async Process GetProductsTest()
var software = new WebApplicationFactory()
.WithWebHostBuilder(builder =>
builder.ConfigureServices(companies =>
services.AddSingleton()
)
)
var customer = application.CreateClient()
var response = await client.GetStringAsync("/getproducts")
Assert.Equal(HttpStatusCode.Alright, response.StatusCode)
You can use the exact code to take a look at applying TestServer or WebApplicationFactory in .Internet 5 and .Internet 6.
Include a logging provider in ASP.Net Main 5
Logging vendors in ASP.Net Main are utilized to keep logs. The default logging providers included in ASP.Internet Main are the Debug, Console, EventLog, and EventSource logging providers.
You can use the ClearProviders method to obvious all logging companies and increase a specific logging service provider or your personal custom logging supplier. The following code snippet illustrates how you can remove all ILoggerProvider circumstances and include the Console logging provider in ASP.Web Main 5.
community static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
logging.ClearProviders()
logging.AddConsole()
)
.ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup()
)
Add a logging company in ASP.Net Main 6
In ASP.Net Core 6, when you get in touch with WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging suppliers. The next code snippet displays how you can apparent the default logging vendors and increase only the Console logging provider in ASP.Net Main 6.
var builder = WebApplication.CreateBuilder(args)
//Clear default logging providers
builder.Logging.ClearP roviders()
//Code to insert providers to the container
builder.Logging.AddConsole()
var app = builder.Establish()
The code illustrations supplied here illustrate the unique methods we increase middleware, routing, companies, and logging vendors in ASP.Internet Main 5 and in ASP.Web Main 6, as effectively as variations in the Software course and tests. These snippets ought to support you when functioning with ASP.Net Main 6 applications, and get you off to a excellent commence when you migrate your ASP.Web Main 5 purposes to ASP.Web Main 6.
Copyright © 2022 IDG Communications, Inc.
[ad_2]
Resource website link