没有为“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型提供服务

我遇到了这个问题: 没有注册“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型的服务。 在asp.net核心1.0中,似乎当动作尝试渲染视图时我有exception。

我搜索了很多,但我没有找到解决方案,如果有人可以帮我弄清楚发生了什么,我怎么能解决它,我会很感激。

我的代码如下:

我的project.json文件

{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.0", "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "EntityFramework.Commands": "7.0.0-rc1-final" }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { "netcoreapp1.0": { "imports": [ "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, "publishOptions": { "include": [ "wwwroot", "web.config" ] }, "scripts": { "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } } 

我的Startup.cs文件

 using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OdeToFood.Services; namespace OdeToFood { public class Startup { public IConfiguration configuration { get; set; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddScoped(); services.AddMvcCore(); services.AddSingleton(provider => configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //app.UseRuntimeInfoPage(); app.UseFileServer(); app.UseMvc(ConfigureRoutes); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } private void ConfigureRoutes(IRouteBuilder routeBuilder) { routeBuilder.MapRoute("Default", "{controller=Home}/{action=Index}/{id?}"); } } } 

解决方案:Startup.cs使用AddMvc()而不是AddMvcCore() ,它将起作用。

有关原因的详细信息,请参阅此问题:

对于大多数用户,将不会有任何更改,您应该继续在启动代码中使用AddMvc()和UseMvc(…)。

对于真正勇敢的人来说,现在有一种配置体验,您可以从最小的MVC管道开始,并添加function以获得自定义框架。

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

您可能还需要在project.json添加对Microsoft.AspNetCore.Mvc.ViewFeature的引用

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

如果您使用的是2.0那么请使用services.AddMvcCore().AddRazorViewEngine(); 在您的ConfigureServices

如果您使用的是Authorize属性,请记得添加.AddAuthorization() ,否则它将无效。

只需添加以下代码即可:

  public void ConfigureServices(IServiceCollection services) { services.AddMvcCore() .AddViews(); } 

对于那些在.NetCore 1.X – > 2.0升级期间遇到此问题的人,请更新Program.csStartup.cs

 public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() .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(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddTransient(); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // no change to this method leave yours how it is } } 

这适用于我的情况:

 services.AddMvcCore() .AddApiExplorer();