专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:00:52       共计:3652 浏览



.NET Core 的版本是 3.1

遇到的问题是 Action 中 return View() 的时候报错

An unhandled exception occurred while processing the request.

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.


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>();


这一段绑定的代码。




版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:ASP.NET Corehtml输出的中文被编码了,解决编码问题 | ·下一条:浅谈ASP.NET Core中的DI

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有