From 0303c9e90b5b48b309a78c2ec9911db1812e6bf3 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sun, 21 Apr 2019 09:51:41 -0700 Subject: [PATCH] AddControllersWithViewsCore should add CacheTagHelper services (#9580) --- .../Mvc/src/MvcServiceCollectionExtensions.cs | 5 ++- .../MvcServiceCollectionExtensionsTest.cs | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs b/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs index ffb69ee5041c..1dc105ee9d78 100644 --- a/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs +++ b/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs @@ -227,7 +227,10 @@ public static IMvcBuilder AddControllersWithViews(this IServiceCollection servic private static IMvcCoreBuilder AddControllersWithViewsCore(IServiceCollection services) { - var builder = AddControllersCore(services).AddViews().AddRazorViewEngine(); + var builder = AddControllersCore(services) + .AddViews() + .AddRazorViewEngine() + .AddCacheTagHelper(); AddTagHelpersFrameworkParts(builder.PartManager); diff --git a/src/Mvc/Mvc/test/MvcServiceCollectionExtensionsTest.cs b/src/Mvc/Mvc/test/MvcServiceCollectionExtensionsTest.cs index c7dadf76b5a3..b20563dcfe35 100644 --- a/src/Mvc/Mvc/test/MvcServiceCollectionExtensionsTest.cs +++ b/src/Mvc/Mvc/test/MvcServiceCollectionExtensionsTest.cs @@ -15,12 +15,15 @@ using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Cors; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Razor.TagHelpers; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; +using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Mvc.ViewComponents; +using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures.Filters; using Microsoft.AspNetCore.Routing; @@ -219,6 +222,35 @@ public void AddRazorPages_Twice_DoesNotAddDuplicates() VerifyAllServices(services); } + [Fact] + public void AddControllersWithViews_AddsDocumentedServices() + { + // Arrange + var services = new ServiceCollection(); + services.AddControllersWithViews(); + + // Assert + // Adds controllers + Assert.Contains(services, s => s.ServiceType == typeof(IActionInvokerProvider) && s.ImplementationType == typeof(ControllerActionInvokerProvider)); + // Adds ApiExplorer + Assert.Contains(services, s => s.ServiceType == typeof(IApiDescriptionGroupCollectionProvider)); + // Adds CORS + Assert.Contains(services, s => s.ServiceType == typeof(CorsAuthorizationFilter)); + // Adds DataAnnotations + Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions) && s.ImplementationType == typeof(MvcDataAnnotationsMvcOptionsSetup)); + // Adds FormatterMappings + Assert.Contains(services, s => s.ServiceType == typeof(FormatFilter)); + // Adds Views + Assert.Contains(services, s => s.ServiceType == typeof(IHtmlHelper)); + // Adds Razor + Assert.Contains(services, s => s.ServiceType == typeof(IRazorViewEngine)); + // Adds CacheTagHelper + Assert.Contains(services, s => s.ServiceType == typeof(CacheTagHelperMemoryCacheFactory)); + + // No Razor Pages + Assert.Empty(services.Where(s => s.ServiceType == typeof(IActionInvokerProvider) && s.ImplementationType == typeof(PageActionInvokerProvider))); + } + private void VerifyAllServices(IServiceCollection services) { var singleRegistrationServiceTypes = SingleRegistrationServiceTypes;