Error: No service for type 'ITempDataDictionaryFactory' has been registered

Dung Do Tien Dec 19 2020 700

After convert project Asp.net Core version 2.1 to 3.1 when running web application I got an error InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.

Below is the code of Startup.cs file:

public class Startup
{
    [Obsolete]
    public Startup(Microsoft.AspNetCore.Hosting.IHostingEnvironment evm)
    {
        var builder = new ConfigurationBuilder()
           .SetBasePath(evm.ContentRootPath)
           .AddJsonFile("appsettings.json", true, true)
           .AddJsonFile($"appsettings.{evm.EnvironmentName}.json", true)
           .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    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 = Microsoft.AspNetCore.Http.SameSiteMode.None;
        });

        QDM.CMS.Bsl.AutoMapper.RegisterMapping();
        services.RegistrationIoc();

        services.AddControllers(x => x.AllowEmptyInputInBodyModelBinding = true);
        services.AddRouting();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    [Obsolete]
    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.UseCookiePolicy();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(routes =>
        {
            routes.MapControllerRoute(
                name: "default",
                pattern: "{controller=Admin}/{action=Login}/{id?}");
        });
    }
}

Thank you for any suggestion.

Have 1 answer(s) found.
  • M

    Manish Kumar Dec 19 2020

    I got the same error In Asp.net core 3.1. Add command below at the end of ConfigureServices() method, it will work for you: 

    services.AddControllersWithViews(); 

    Hope it's helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close