简体   繁体   English

没有注册“Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor”类型的服务

[英]No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor' has been registered

I have a ToString() method for Partial Views.我有一个用于局部视图的ToString()方法。

My usings :我的用途:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using System.Threading.Tasks; 

public static class ViewExtensions
{
    public static async Task<string> ToString(this PartialViewResult partialView, ActionContext actionContext)
    {
        using (var writer = new StringWriter())
        {
            var services = actionContext.HttpContext.RequestServices;
            var executor = services.GetRequiredService<PartialViewResultExecutor>();
            var view = executor.FindView(actionContext, partialView).View;
            var viewContext = new ViewContext(actionContext, view, partialView.ViewData, partialView.TempData, writer, new HtmlHelperOptions());
            await view.RenderAsync(viewContext);
            return writer.ToString();
        }
    }
}

In ASP.NET Core 2.2 I am getting this error :在 ASP.NET Core 2.2 中,我收到此错误:

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor' has been registered. System.InvalidOperationException:没有注册“Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor”类型的服务。 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

Startup类的ConfigureServices方法中添加以下行:

services.AddSingleton<PartialViewResultExecutor>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有注册“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型的服务 - No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered 没有注册类型为“Microsoft.AspNetCore.Mvc ...”的服务 - No service for type 'Microsoft.AspNetCore.Mvc..." has been registered 没有为“Microsoft.AspNetCore.Http.HttpContextAccessor”类型注册服务 - No service for type 'Microsoft.AspNetCore.Http.HttpContextAccessor' has been registered ASP.NET 核心 3:InvalidOperationException:没有 Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource 类型的服务已注册 - ASP.NET Core 3: InvalidOperationException: No service for type Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource has been registered 单元测试模拟 ControllerContext HttpContext 没有为类型“Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory”注册服务 - Unit Tests Mock ControllerContext HttpContext No service for type 'Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory' has been registered 没有注册“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered 没有注册 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext' 类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext' has been registered System.InvalidOperationException:“没有为类型 'Microsoft.AspNetCore.Hosting.Server.IServer' 注册服务。” - System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Hosting.Server.IServer' has been registered.' 没有为“Microsoft.AspNetCore.Identity.UserManager`1[testLogin.Areas.Identity.Data.testLoginUser]”类型注册服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[testLogin.Areas.Identity.Data.testLoginUser]' has been registered 没有注册 'Microsoft.AspNetCore.Identity.UserManager`1[System.Model.User]' 类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[System.Model.User]' has been registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM