简体   繁体   中英

How to use 'MapControllers' in ASP.NET Core 2.1

See Routing to controller actions in ASP.NET Core

This doc page can be switched to 2.1. It shows attribute routing instructions.

But, methods like app.UseEndpoints(()=>) , endpoints.MapControllers();or app.MapControllers(); don't exist in my environment.

I've installed these packages:

Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.Mvc
Microsoft.AspNetCore.Routing
Microsoft.AspNetCore.Routing.Abstractions

What else should I install?

But, methods like app.UseEndpoints(()=>), endpoints.MapControllers();or app.MapControllers(); don't exist in my environment.

This is a documentation issue, the Endpoint routing applies to ASP.NET Core 3.0 and later, instead of Asp.net Core 2.1 .

In Asp.net core 2.1 MVC application, you should use the UseMvc middleware and the MapRoute() method, the startup.cs file like this:

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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

If you want to use the Endpoint routing, you could migrate the application from Asp.net core 2.1 to Asp.net core 3.1. Refer to this link and steps .

For the documentation issue, you can submit a feedback by click the button at the end of the document:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM