Configure the ASP.NET Core site to use MVC

In ASP.NET Core 3.0 and later projects, .NET Framework is no longer a supported target framework. Your project must target .NET Core. The ASP.NET Core shared framework, which includes MVC, is part of the .NET Core runtime installation. The shared framework is automatically referenced when using the Microsoft.NET.Sdk.Web SDK in the project file:


XML

<Project Sdk="Microsoft.NET.Sdk.Web">
For more information, see Framework reference.

In ASP.NET Core, the Startup class:

Replaces Global.asax.
Handles all app startup tasks.
For more information, see App startup in ASP.NET Core.

In the ASP.NET Core project, open the Startup.cs file:

C#

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
ASP.NET Core apps must opt in to framework features with middleware. The previous template-generated code adds the following services and middleware:

The AddControllersWithViews extension method registers MVC service support for controllers, API-related features, and views. 
The UseStaticFiles extension method adds the static file handler Microsoft.AspNetCore.StaticFiles. The UseStaticFiles extension method must be called before UseRouting
The UseRouting extension method adds routing. 
This existing configuration includes what is needed to migrate the example ASP.NET MVC project. 

Source: docs.microsoft.com

Post a Comment

0 Comments