// Copyright (c) .NET Foundation. All rights reserved.// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Reflection;using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.ApplicationParts;using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;using Microsoft.AspNetCore.Mvc.TagHelpers;namespace Microsoft.Extensions.DependencyInjection
{/// <summary>/// Extension methods for setting up MVC services in an <see cref="IServiceCollection" />./// </summary>publicstaticclassMvcServiceCollectionExtensions{/// <summary>/// Adds MVC services to the specified <see cref="IServiceCollection" />./// </summary>/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>publicstaticIMvcBuilderAddMvc(thisIServiceCollection services){if(services ==null){thrownewArgumentNullException(nameof(services));}var builder = services.AddMvcCore();
builder.AddApiExplorer();
builder.AddAuthorization();AddDefaultFrameworkParts(builder.PartManager);// Order added affects options setup order// Default framework order
builder.AddFormatterMappings();
builder.AddViews();
builder.AddRazorViewEngine();
builder.AddRazorPages();
builder.AddCacheTagHelper();// +1 order
builder.AddDataAnnotations();// +1 order
builder.AddCors();returnnewMvcBuilder(builder.Services, builder.PartManager);}privatestaticvoidAddDefaultFrameworkParts(ApplicationPartManager partManager){var mvcTagHelpersAssembly =typeof(InputTagHelper).GetTypeInfo().Assembly;if(!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcTagHelpersAssembly)){
partManager.ApplicationParts.Add(newFrameworkAssemblyPart(mvcTagHelpersAssembly));}var mvcRazorAssembly =typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;if(!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcRazorAssembly)){
partManager.ApplicationParts.Add(newFrameworkAssemblyPart(mvcRazorAssembly));}}/// <summary>/// Adds MVC services to the specified <see cref="IServiceCollection" />./// </summary>/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>publicstaticIMvcBuilderAddMvc(thisIServiceCollection services, Action<MvcOptions> setupAction){if(services ==null){thrownewArgumentNullException(nameof(services));}if(setupAction ==null){thrownewArgumentNullException(nameof(setupAction));}var builder = services.AddMvc();
builder.Services.Configure(setupAction);return builder;}[DebuggerDisplay("{Name}")]privateclassFrameworkAssemblyPart:AssemblyPart, ICompilationReferencesProvider
{publicFrameworkAssemblyPart(Assembly assembly):base(assembly){}
IEnumerable<string> ICompilationReferencesProvider.GetReferencePaths()=> Enumerable.Empty<string>();}}}