Emm*_*gas 29 c# asp.net asp.net-mvc asp.net-core-1.0
我遇到了这个问题:没有注册"Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory"类型的服务.在asp.net核心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%" ]
}
}
Run Code Online (Sandbox Code Playgroud)
我的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?}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*ved 46
解决方案:使用AddMvc()而不是AddMvcCore()in Startup.cs,它将起作用.
有关原因的详细信息,请参阅此问题:
对于大多数用户,将不会有任何更改,您应该继续在启动代码中使用AddMvc()和UseMvc(...).
对于真正勇敢的人来说,现在有一种配置体验,您可以从最小的MVC管道开始,并添加功能以获得自定义框架.
您可能还需要添加对Microsoft.AspNetCore.Mvc.ViewFeaturein 的引用project.json
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.ViewFeatures/
小智 28
我知道这是一篇旧帖子,但在将 MVC 项目迁移到 .NET Core 3.0 后遇到此问题时,这是我在 Google 上的最高结果。让我Startup.cs看起来像这样为我修复了它:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Run Code Online (Sandbox Code Playgroud)
CoO*_*oOl 24
如果您正在使用,2.0那么请使用services.AddMvcCore().AddRazorViewEngine();您的ConfigureServices
.AddAuthorization()如果你正在使用Authorize属性,请记得添加,否则它将无法正常工作.
h-r*_*rai 11
在 .NET Core 3.1 中,我必须添加以下内容:
services.AddRazorPages();
Run Code Online (Sandbox Code Playgroud)
在 ConfigureServices()
而下面Configure()的Startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
}
Run Code Online (Sandbox Code Playgroud)