2016-08-01 91 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屬性,否則它將不起作用。

相關問題