AutoMapper在aspnetcore5.0.2中无法工作”
创始人
2024-09-23 15:30:30
0

在Startup类的ConfigureServices方法中添加以下代码:

services.AddAutoMapper(typeof(Startup));

并在项目中安装AutoMapper.Extensions.Microsoft.DependencyInjection包,代码示例如下:

using AutoMapper; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using WebApplication1.Data; using WebApplication1.Models;

namespace WebApplication1 { 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();

        services.AddDbContext(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddAutoMapper(typeof(Startup));
    }

    // 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();
            app.UseMigrationsEndPoint();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

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

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

此时,AutoMapper应该在asp net core 5.0.2中可以正常工作了。

相关内容

热门资讯

第7分钟实锤!德州ai辅助(德... 第7分钟实锤!德州ai辅助(德州ai)本来有挂(详细辅助必赢方法)1、超多福利:超高返利,海量正版游...
第1分钟实锤!智星德州菠萝有挂... 第1分钟实锤!智星德州菠萝有挂吗(德州ai)真是存在有挂(详细辅助存在挂教程)1.智星德州菠萝有挂吗...
第4分钟实锤!德州之星有辅助挂... 第4分钟实锤!德州之星有辅助挂(来玩德州)原来存在有挂(详细辅助曝光教程)1)德州之星有辅助挂辅助挂...
第十分钟实锤!德扑ai智能机器... 第十分钟实锤!德扑ai智能机器人(nzt德州)好像是有挂(详细辅助wepoke教程);1、用户打开应...
第三分钟实锤!德州微扑克辅助(... 第三分钟实锤!德州微扑克辅助(智星德州)确实真的是有挂(详细辅助wpk教程)1、任何德州微扑克辅助a...
第八分钟实锤!德州之星有辅助挂... 第八分钟实锤!德州之星有辅助挂(德州nzt)都是存在有挂(详细辅助细节揭秘)1)德州之星有辅助挂辅助...
七分钟实锤!德州微扑克辅助(来... 七分钟实锤!德州微扑克辅助(来玩德州)好像真的是有挂(详细辅助软件教程)1、起透看视 德州微扑克辅助...
第十分钟实锤!智星德州菠萝外挂... 第十分钟实锤!智星德州菠萝外挂(德州机器人)都是真的是有挂(详细辅助玩家教程);1、全新机制【智星德...
3分钟实锤!德州之星有辅助挂(... 3分钟实锤!德州之星有辅助挂(德州wepower)切实是有挂(详细辅助AI教程);1、德州之星有辅助...
第四分钟实锤!德州微扑克辅助(... 第四分钟实锤!德州微扑克辅助(德州之星)确实真的是有挂(详细辅助必胜教程)1、每一步都需要思考,不同...