c# - 没有注册类型 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' 的服务

标签 c# asp.net asp.net-mvc asp.net-core-1.0

我遇到了这个问题:没有注册 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' 类型的服务。 在 asp.net core 1.0 中,似乎当操作尝试时渲染 View 我有那个异常(exception)。

我搜索了很多,但没有找到解决方案,如果有人可以帮助我弄清楚发生了什么以及如何解决它,我将不胜感激。

我的代码如下:

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

最佳答案

解决方案:Startup.cs 中使用 AddMvc() 而不是 AddMvcCore() 即可.

有关原因的更多信息,请参阅此问题:

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

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

中的Microsoft.AspNetCore.Mvc.ViewFeature

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

关于c# - 没有注册类型 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709538/

相关文章:

c# - 如何使用 asp.net mvc3 和 nhibernate 提高 Web 应用程序性能

c# - 如果系统日期错误,如何获取正确的日期

c# - 从 SAML token 读取 SAML 属性

c# - XmlSerializer 'Compiling JScript/CSharp scripts is not supported'

c# - 在我自己的页面中显示 ELMAH 错误详细信息?

c# - 在Visual Studio 7.5 Mac中找不到指定的框架 'Microsoft.AspNetCore.App'和版本 '2.1.0' docker

c# - Visual Studio Razor 页面错误语法错误

asp.net - asp .net 一个控件的多个验证器

c# - 无法访问 Web 控件的 Page_Load 事件中的公共(public)方法

c# - 在 asp.net mvc 6 Entity Framework 7 中获取 session 值 OnConfiguring