我遇到此問題:沒有爲'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?}");
}
}
}
這是發生在特定頁面嗎?你想在那個網頁上做什麼?當你註釋掉IRestaurantData註冊時,你是否能夠複製這個問題? – Shyju
@Shyju,感謝您的重播,這只是當我嘗試調用View()方法在我的homeController的任何操作中顯示視圖時發生,總是我不重載該方法它總是拋出異常,當我評論'IRestaurantData'註冊服務的問題仍在發生,這是很奇怪的問題:(因爲它似乎我缺少一些命名空間orsomething但vs不顯示我在代碼中的任何錯誤 –
@Shyju這是命名空間我「M使用: '使用Microsoft.AspNetCore.Mvc;'' 使用OdeToFood.ViewModels;'' 使用OdeToFood.Services;'' 使用OdeToFood.Entities;' –