No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered
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/
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();
I know this is an old post but it was my top Google result when running into this after migrating an MVC project to .NET Core 3.0. Making my Startup.cs
look like this fixed it for me:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}