我遇到了这个问题:asp中有No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.个.net core 1.0,似乎当动作试图呈现视图时,我有一个例外.

我已经搜索了很多,但我没有找到解决这个问题的方法,如果有人能帮我弄清楚发生了什么,我该如何修复它,我将不胜感激.

我的代码如下:

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

推荐答案

Solution:使用AddMvc()而不是Startup.cs中的AddMvcCore(),它会起作用.

有关原因的更多信息,请参见本期:

对于大多数用户来说,不会有任何更改,您应该继续使用 AddMvc()和UseMvc(.)在您的启动代码中.

对于真正勇敢的人来说,现在有一个配置体验

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

您可能还需要添加一个引用

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

Asp.net相关问答推荐

如何从 Azure 上托管的应用服务获取登录用户名?

如何防止 IISExpress 和我的网站文件夹弄乱我的文档文件夹?

在 ASP.Net 中使用 Page_Load 和 Page_PreRender

为什么微软决定支持 jQuery 如此重要?

我可以创建 .config 文件并将其包含到 web.config 中吗?

Httpclient 该实例已经启动了一个或多个请求.只能在发送第一个请求之前修改属性

无法确定条件表达式的类型,因为 'string' 和 'System.DBNull' 之间没有隐式转换

ASP.net 与 PHP( Select 什么)

如何使用 app_GlobalResource 或 app_LocalResource?

如何以编程方式将标签的前景色设置为其默认值?

asp.net 有控制台日志(log)吗?

使用 ASP.NET Web API 对 PUT 和 DELETE 的 CORS 支持

如何防止 XXE 攻击(.NET 中的 XmlDocument)

HtmlAgilityPack Select 的子 node 不符合预期

会员生成密码 仅限字母数字密码?

将 html 标记从 jquery 发布调用发送到 asp.net 页面时,从客户端检测到潜在危险的 Request.QueryString 值

ASP.net 网站中的 C# 密码文本框

IIS 是否执行非法字符替换?如果是这样,如何阻止它?

在 Asp.Net MVC 5 中获取登录用户的用户 ID

在 Visual Studio 2012 中调试 .NET Framework 源代码?