2016-08-01 520 views
17

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

我搜索了很多,但我没有找到解决方案,如果有人可以帮助我弄清楚发生了什么,我该如何解决它,我将不胜感激。

我的代码波纹管:

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<IRestaurantData, InMemoryRestaurantData>(); 
      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?}"); 
     } 
    } 
} 
+0

这是发生在特定页面吗?你想在那个网页上做什么?当你注释掉IRestaurantData注册时,你是否能够复制这个问题? – Shyju

+0

@Shyju,感谢您的重播,这只是当我尝试调用View()方法在我的homeController的任何操作中显示视图时发生,总是我不重载该方法它总是抛出异常,当我评论'IRestaurantData'注册服务的问题仍在发生,这是很奇怪的问题:(因为它似乎我缺少一些命名空间orsomething但vs不显示我在代码中的任何错误 –

+0

@Shyju这是命名空间我“M使用: '使用Microsoft.AspNetCore.Mvc;'' 使用OdeToFood.ViewModels;'' 使用OdeToFood.Services;'' 使用OdeToFood.Entities;' –

回答

32

解决方案:使用AddMvc()代替AddMvcCore()Startup.cs,它会工作。

请看到这个问题了,为什么更多的信息:

对于大多数用户不会有任何改变,你应该继续在你的启动代码中使用 AddMvc()和UseMvc(...) 。

为了真正的勇敢,现在有一个配置体验,您可以从最小的MVC流水线开始,并添加功能来获得 定制框架。

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

您可能还需要参考 在project.json

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

0

添加到Microsoft.AspNetCore.Mvc.ViewFeature只需添加以下代码,它应该工作:

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

     } 
0

佛[R那些.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<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 
    } 
} 
4

如果您使用2.0然后用services.AddMvcCore().AddRazorViewEngine();ConfigureServices

还记得加.AddAuthorization()如果您使用Authorize属性,否则它将不起作用。