No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered

前端 未结 11 778
终归单人心
终归单人心 2021-01-01 08:57

I\'m having this problem: No service for type \'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory\' has been registered. In asp.net core 1.0,

相关标签:
11条回答
  • 2021-01-01 09:17

    Solution: Use services.AddMvcCore(options => options.EnableEndpointRouting = false).AddRazorViewEngine(); in Startup.cs and it will work.

    This code is tested for asp.net core 3.1 (MVC)

    0 讨论(0)
  • 2021-01-01 09:20

    In .NET Core 3.1, I had to add the following:

    services.AddRazorPages();
    

    in ConfigureServices()

    And the below in Configure() in Startup.cs

    app.UseEndpoints(endpoints =>
    {
         endpoints.MapRazorPages();
    }
    
    0 讨论(0)
  • 2021-01-01 09:26

    If you're using 2.x then use services.AddMvcCore().AddRazorViewEngine(); in your ConfigureServices

    Also remember to add .AddAuthorization() if you're using Authorize attribute, otherwise it won't work.

    Update: for 3.1 onwards use services.AddControllersWithViews();

    0 讨论(0)
  • 2021-01-01 09:28

    Solution: Use AddMvc() instead of AddMvcCore() in Startup.cs and it will work.

    Please see this issue for further information about why:

    For most users there will be no change, and you should continue to use AddMvc() and UseMvc(...) in your startup code.

    For the truly brave, there's now a configuration experience where you can start with a minimal MVC pipeline and add features to get a customized framework.

    https://github.com/aspnet/Mvc/issues/2872

    You might also have to add a reference toMicrosoft.AspNetCore.Mvc.ViewFeature in project.json

    https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.ViewFeatures/

    0 讨论(0)
  • 2021-01-01 09:29

    For those that get this issue during .NetCore 1.X -> 2.0 upgrade, update both your Program.cs and Startup.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
    
    public class Startup
    {
    // The appsettings.json settings that get passed in as Configuration depends on 
    // project properties->Debug-->Enviroment Variables-->ASPNETCORE_ENVIRONMENT
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
    
            services.AddTransient<IEmailSender, EmailSender>();
    
            services.AddMvc();
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
        // no change to this method leave yours how it is
        }
    }
    
    0 讨论(0)
提交回复
热议问题