我遇到了这个问题: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相关问答推荐

如何格式化搜索字符串以从 Razor 页表中的多个列返回部分搜索字符串?

我可以强制刷新我的样式表文件吗?

Dotnet core 2.0 身份验证多模式身份 cookie 和 jwt

有条件地在 Gridview 中隐藏 CommandField 或 ButtonField

登录成功后 User.Identity.IsAuthenticated 为 false

加载特定 UpdatePanel 后如何调用客户端 javascript 函数

如何缓存数据库表以防止在 Asp.net C# mvc 中出现许多数据库查询

__doPostBack 未定义

无法加载文件或程序集'System.Web.WebPages.Razor,版本 = 3.0.0.0

从 IIS 7/8 中的静态内容中删除服务器标头

反序列化客户端 AJAX JSON 日期

如何在新实现的接口或基类之间做出决定?

如何设置默认页面 asp.net

将 Http 请求读入字节数组

从 IFrame 重定向父页面

Web API 必填参数

当文件上传超出 ASP.NET MVC 中允许的大小时显示自定义错误页面

回发后运行javascript函数

在 ASPNET 标识 2.0 中禁用用户

~/ 等价于 javascript