Pas de service pour le type " Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory " a été enregistré
j'ai ce problème: pas de service pour le type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory " a été enregistré.<!--4 - asp.net core 1.0, il semble que lorsque l'action essaye de rendre la vue j'ai cette exception.
j'ai beaucoup cherché mais je n'ai pas trouvé de solution à cela, si quelqu'un peut m'aider à comprendre ce qui se passe et comment puis-je le réparer, je l'apprécierai.
Mon code ci-dessous:
<!-- 2 - projet.json le fichier{
"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%" ]
}
}
Démarrage.cs le fichier
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?}");
}
}
}
5 réponses
Solution: Utiliser AddMvc() au lieu de AddMvcCore()Startup.cs et il va fonctionner.
s'il vous Plaît voir cette question pour de plus amples informations sur le pourquoi:
Pour la plupart des utilisateurs n'y aura aucun changement, et vous devez continuer à utiliser AddMvc () et UseMvc (...) dans votre code de démarrage.
pour les vraiment courageux, il y a maintenant une expérience de configuration où vous peut commencer avec un pipeline MVC minimal et ajouter des fonctionnalités pour obtenir personnaliser Framework.
Vous pouvez également ajouter une référence
Microsoft.AspNetCore.Mvc.ViewFeatureproject.json
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.ViewFeatures/
Si vous utilisez 2.0 puis utilisez services.AddMvcCore().AddRazorViewEngine(); dans votre ConfigureServices
Aussi, n'oubliez pas d'ajouter .AddAuthorization() si vous utilisez Authorize l'attribut, sinon ça ne marchera pas.
Juste ajouter le code suivant et cela devrait fonctionner:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddViews();
}
Pour ceux qui obtiennent cette question au cours de .NetCore 1.X -> 2.0 mise à niveau, mise à jour de vos deux Program.cs et Startup.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
}
}
celui-ci fonctionne pour mon cas :
services.AddMvcCore()
.AddApiExplorer();