2016-08-01 4 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, 재생 주셔서 감사합니다, 그냥 내가 내 homeController의 행동에보기를 표시하기 위해 View() 메소드를 호출하려고 할 때 발생합니다. 항상 메소드를 오버로드하지 않습니다. 예외를 던지십시오. 'IRestaurantData' 등록 서비스 문제가 여전히 일어나고 있습니다. 그래서 이상한 일입니다. (왜냐하면 내가 네임 스페이스 orsomething을 놓치고있는 것처럼 보이기 때문입니다. 그러나 대요. 코드에서 아무 것도 보여주지 않습니다. –

+0

@Shyju 이것은 네임 스페이스입니다. 'M using : 'Microsoft.AspNetCore.Mvc;를 사용하여 OdeToFood.ViewModels; –

답변

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

FO R .NetCore의 1.X 동안이 문제를 얻을 것들 -> 2.0 업그레이드, 업데이트 모두 당신의 Program.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

Startup.cs 당신이 다음에 services.AddMvcCore().AddRazorViewEngine();를 사용 2.0를 사용하는 경우 ConfigureServices 또한

추가해야합니다 이면 Authorize 속성을 사용합니다. 그렇지 않으면 작동하지 않습니다.

관련 문제