没有注册类型'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory'的服务 [英] No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered

查看:450
本文介绍了没有注册类型'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory'的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题:没有注册类型为'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory'的服务.在asp.net core 1.0中,似乎该操作尝试了渲染视图我有那个例外.

I'm having this problem: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered. In asp.net core 1.0, it seems that when the action try to render the view i have that exception.

我进行了很多搜索,但是我没有找到解决方案,如果有人可以帮助我弄清楚正在发生的事情以及如何解决它,我将不胜感激.

I've searched a lot but I dont found a solution to this, if somebody can help me to figure out what's happening and how can I fix it, i will appreciate it.

下面的代码:

我的 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?}");
        }
    }
}

推荐答案

解决方案:使用AddMvc()代替Startup.cs中的AddMvcCore(),它将起作用.

Solution: Use AddMvc() instead of AddMvcCore() in Startup.cs and it will work.

请查看此问题以获取有关原因的更多信息:

Please see this issue for further information about why:

对于大多数用户来说,没有任何变化,您应该继续使用 在您的启动代码中添加AddMvc()和UseMvc(...).

For most users there will be no change, and you should continue to use AddMvc() and UseMvc(...) in your startup code.

为了真正勇敢,现在有了配置体验,您可以 可以从最小的MVC管道开始,并添加功能以获取 定制的框架.

For the truly brave, there's now a configuration experience where you can start with a minimal MVC pipeline and add features to get a customized framework.

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

您可能还需要添加参考 到project.json

You might also have to add a reference toMicrosoft.AspNetCore.Mvc.ViewFeature in project.json

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

这篇关于没有注册类型'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory'的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆