Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.”
.NET Core 的版本是 3.1
遇到的问题是 Action 中 return View() 的时候报错
An unhandled exception occurred while processing the request.
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
错误内容的字面意思是 ITempDataDictionaryFactory 这玩意没有注册。
解决方案一:
修改 Startup.cs 中的 ConfigureServices方法
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
解决方案二:
修改 Startup.cs 中的 ConfigureServices方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
查看AspNetCore.Mvc 的源码
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} services.AddControllersWithViews();
return services.AddRazorPages();
}
AddMvc 方法中包含了 AddControllersWithViews
public static IMvcBuilder AddControllersWithViews(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} var builder = AddControllersWithViewsCore(services);
return new MvcBuilder(builder.Services, builder.PartManager);
}
跳转到 AddControllersWithViewsCore
private static IMvcCoreBuilder AddControllersWithViewsCore(IServiceCollection services)
{
var builder = AddControllersCore(services)
.AddViews()
.AddRazorViewEngine()
.AddCacheTagHelper(); AddTagHelpersFrameworkParts(builder.PartManager); return builder;
}
跳转到 AddViews
public static IMvcCoreBuilder AddViews(this IMvcCoreBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
} builder.AddDataAnnotations();
AddViewComponentApplicationPartsProviders(builder.PartManager);
AddViewServices(builder.Services);
return builder;
}
跳转到 AddViewServices
internal static void AddViewServices(IServiceCollection services)
{
services.AddDataProtection();
services.AddAntiforgery();
services.AddWebEncoders(); services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcViewOptions>, MvcViewOptionsSetup>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, TempDataMvcOptionsSetup>()); //
// View Engine and related infrastructure
//
services.TryAddSingleton<ICompositeViewEngine, CompositeViewEngine>();
services.TryAddSingleton<IActionResultExecutor<ViewResult>, ViewResultExecutor>();
services.TryAddSingleton<IActionResultExecutor<PartialViewResult>, PartialViewResultExecutor>(); // Support for activating ViewDataDictionary
services.TryAddEnumerable(
ServiceDescriptor
.Transient<IControllerPropertyActivator, ViewDataDictionaryControllerPropertyActivator>()); //
// HTML Helper
//
services.TryAddTransient<IHtmlHelper, HtmlHelper>();
services.TryAddTransient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>));
services.TryAddSingleton<IHtmlGenerator, DefaultHtmlGenerator>();
services.TryAddSingleton<ModelExpressionProvider>();
// ModelExpressionProvider caches results. Ensure that it's re-used when the requested type is IModelExpressionProvider.
services.TryAddSingleton<IModelExpressionProvider>(s => s.GetRequiredService<ModelExpressionProvider>());
services.TryAddSingleton<ValidationHtmlAttributeProvider, DefaultValidationHtmlAttributeProvider>(); services.TryAddSingleton<IJsonHelper, SystemTextJsonHelper>(); // Component services for Blazor server-side interop
services.TryAddSingleton<ServerComponentSerializer>(); //
// View Components
// // These do caching so they should stay singleton
services.TryAddSingleton<IViewComponentSelector, DefaultViewComponentSelector>();
services.TryAddSingleton<IViewComponentFactory, DefaultViewComponentFactory>();
services.TryAddSingleton<IViewComponentActivator, DefaultViewComponentActivator>();
services.TryAddSingleton<
IViewComponentDescriptorCollectionProvider,
DefaultViewComponentDescriptorCollectionProvider>();
services.TryAddSingleton<IActionResultExecutor<ViewComponentResult>, ViewComponentResultExecutor>(); services.TryAddSingleton<ViewComponentInvokerCache>();
services.TryAddTransient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>();
services.TryAddSingleton<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>();
services.TryAddTransient<IViewComponentHelper, DefaultViewComponentHelper>(); //
// Temp Data
//
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, TempDataApplicationModelProvider>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, ViewDataAttributeApplicationModelProvider>());
services.TryAddSingleton<SaveTempDataFilter>(); //
// Component rendering
//
services.TryAddScoped<IComponentRenderer, ComponentRenderer>();
services.TryAddScoped<StaticComponentRenderer>();
services.TryAddScoped<NavigationManager, HttpNavigationManager>();
services.TryAddScoped<IJSRuntime, UnsupportedJavaScriptRuntime>();
services.TryAddScoped<INavigationInterception, UnsupportedNavigationInterception>(); services.TryAddTransient<ControllerSaveTempDataPropertyFilter>(); // This does caching so it should stay singleton
services.TryAddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.TryAddSingleton<TempDataSerializer, DefaultTempDataSerializer>(); //
// Antiforgery
//
services.TryAddSingleton<ValidateAntiforgeryTokenAuthorizationFilter>();
services.TryAddSingleton<AutoValidateAntiforgeryTokenAuthorizationFilter>(); // These are stateless so their lifetime isn't really important.
services.TryAddSingleton<ITempDataDictionaryFactory, TempDataDictionaryFactory>();
services.TryAddSingleton(ArrayPool<ViewBufferValue>.Shared);
services.TryAddScoped<IViewBufferScope, MemoryPoolViewBufferScope>();
}
找到了 services.TryAddSingleton<ITempDataDictionaryFactory, TempDataDictionaryFactory>();
这一段绑定的代码。
Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.”的更多相关文章
- TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.Internal.IHttpResponseStreamWriterFactory' from assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.2.0 ...
今天调试 asp.net core 2.0 项目时遇到了如下错误: TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.I ...
- InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'
在新建asp.net core 应用后, 添加了自定义的ApplicationDbContext 和ApplicationUser ,并添加了Identity认证后, 会出现 InvalidOpera ...
- Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...
最近老大让我看Orchard Core,这是一个CMS系统.可以先参考大佬的文章:https://www.cnblogs.com/shanyou/archive/2018/09/25/9700422. ...
- .net core 3.0web_razor page项目_使用中间件接受大文件上传报错_httpRequest.Form threw an exception of type Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException_Request body too large
前言:在web项目的.net framework时文件上传时,自己常用一般处理程序接受上传文件,上传文件的大小限制是可以项目的webconfig里配置. 到core项目使用一般处理程序变成了中间件 ...
- API服务版本控制 Microsoft.AspNetCore.Mvc.Versioning
我们在进行webapi服务开发时,会遇到一些多个版本的api共存的情况发生,例如某一版本APP上线后,需求发生变更,需要在下一个升级版本更新API,但同时又需要保证这个APP版本能正常使用,这时候就需 ...
- MVC中异常: An exception of type 'System.Data.ProviderIncompatibleException' occurred in EntityFramework.dll的一种解决办法
今天在调试MVC的例子的时候,总是出错(An exception of type 'System.Data.ProviderIncompatibleException' occurred in Ent ...
- 由ASP.NET Core根据路径下载文件异常引发的探究
前言 最近在开发新的项目,使用的是ASP.NET Core6.0版本的框架.由于项目中存在文件下载功能,没有使用类似MinIO或OSS之类的分布式文件系统,而是下载本地文件,也就是根据本地文件路径进行 ...
- .NET Core整合log4net以及全局异常捕获实现
在使用log4net之前先安装log4net.这里操作很简单,通过nuget下载并安装log4net很方便.如下图. log4net配置 <?xml version="1.0" ...
- ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)
在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...
随机推荐
- Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...
- WebRTC下 的 NAT 穿透技术
NAT的概念模型 NAT名字很准确,网络地址转换,就是替换IP报文头部的地址信息.NAT通常部署在一个组织的网络出口位置,通过将内部网络IP地址替换为出口的IP地址提供公网可达性和上层协议的连接能力. ...
- 由于ios由UIWebView换成了WKWebview内核后导致webview请求接口文件上传,后台接收不到文件
2020年4月起App Store将不再接受使用UIWebView的新App上架.2020年12月起将不再接受使用UIWebView的App更新. 解决后台文件接收不到的问题 function GLA ...
- Maven常用参数说明
缩写 全名 说明 -h --help 显示帮助信息 -am --also-make 构建指定模块,同时构建指定模块依赖的其他模块 -amd --also-make-dependents 构建指定模块, ...
- MATLAB 错误之生成step图表出错
m文件: step(Gi_close) hold on 错误示例: 使用step函数生成图表后,报错如下: Plots must be of the same type and size to be ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 初识python 之 爬虫:使用正则表达式爬取“古诗文”网页数据
通过requests.re(正则表达式) 爬取"古诗文"网页数据. 详细代码如下: #!/user/bin env python # author:Simple-Sir # tim ...
- 【PowerShell】ASCII与Char之间的转换
1 [char[]][int[]]$char=65..90 2 $char -join ',' 3 [int[]][char[]]$ascii=$char 4 $ascii -join ',' A,B ...
- JSch Algorithm negotiation fail
https://stackoverflow.com/questions/30846076/jsch-algorithm-negotiation-fail As you can see, the ser ...
- 生产环境上,哨兵模式集群Redis版本升级应用实战
背景: 由于生产环境上所使用的Redis版本并不一致,好久也没有更新,为了避免版本不同对Redis集群造成影响,从而升级为统一Redis版本! 1.集群架构 一主两从三哨兵: 2.升级方案 (1)升级 ...