AspNetCore原始碼之MvcServiceCollectionExtensions.cs
// 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>
public static class MvcServiceCollectionExtensions
{
/// <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>
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(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();
return new MvcBuilder(builder.Services, builder.PartManager);
}
private static void AddDefaultFrameworkParts(ApplicationPartManager partManager)
{
var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
if (!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcTagHelpersAssembly))
{
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcTagHelpersAssembly));
}
var mvcRazorAssembly = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
if (!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcRazorAssembly))
{
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(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>
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
var builder = services.AddMvc();
builder.Services.Configure(setupAction);
return builder;
}
[DebuggerDisplay("{Name}")]
private class FrameworkAssemblyPart : AssemblyPart, ICompilationReferencesProvider
{
public FrameworkAssemblyPart(Assembly assembly)
: base(assembly)
{
}
IEnumerable<string> ICompilationReferencesProvider.GetReferencePaths() => Enumerable.Empty<string>();
}
}
}
相關文章
- AspNetCore原始碼之ControllerBase.cs類NetCore原始碼Controller
- AspNetCore原始碼之WebHost.cs類NetCore原始碼Web
- AspNetCore7.0原始碼解讀之UseMiddlewareNetCore原始碼
- 原始碼|jdk原始碼之HashMap分析(一)原始碼JDKHashMap
- 原始碼|jdk原始碼之HashMap分析(二)原始碼JDKHashMap
- Guava 原始碼分析之 EventBus 原始碼分析Guava原始碼
- Android 原始碼分析之 AsyncTask 原始碼分析Android原始碼
- 死磕 jdk原始碼之HashMap原始碼分析JDK原始碼HashMap
- Spring原始碼之IOC(一)BeanDefinition原始碼解析Spring原始碼Bean
- Android 原始碼分析之 EventBus 的原始碼解析Android原始碼
- 原始碼分析之 HashMap原始碼HashMap
- 原始碼分析之 LinkedList原始碼
- Kafka之Producer原始碼Kafka原始碼
- async原始碼之series原始碼
- Srping原始碼之XMLBeanFactory原始碼XMLBean
- Spring原始碼分析——spring原始碼之obtainFreshBeanFactory()介紹Spring原始碼AIBean
- LevelDB 原始碼解析之 Varint 編碼原始碼
- 【原始碼閱讀】Glide原始碼閱讀之with方法(一)原始碼IDE
- 原始碼|jdk原始碼之棧、佇列及ArrayDeque分析原始碼JDK佇列
- 【原始碼閱讀】Glide原始碼閱讀之into方法(三)原始碼IDE
- 【spring 原始碼】IOC 之 ClassPathXmlApplicationContextSpring原始碼XMLAPPContext
- lodash原始碼分析之isArguments原始碼
- 原始碼衝浪之HashMap原始碼HashMap
- Envoy原始碼分析之Dispatcher原始碼
- Dubbo原始碼解析之SPI原始碼
- Fresco原始碼分析之DraweeView原始碼View
- Netty原始碼分析之LengthFieldBasedFrameDecoderNetty原始碼
- RecyclerView之SnapHelper原始碼分析View原始碼
- Vue原始碼解析之nextTickVue原始碼
- tornado 原始碼之 coroutine 分析原始碼
- lodash原始碼分析之isObjectLike原始碼Object
- OpenGL 之 GPUImage 原始碼分析GPUUI原始碼
- java原始碼 - ReentrantLock之FairSyncJava原始碼ReentrantLockAI
- java原始碼-ReentrantLock之FairSyncJava原始碼ReentrantLockAI
- java原始碼-ReentrantLock之NonfairSyncJava原始碼ReentrantLockAI
- jQuery原始碼學習之$()jQuery原始碼
- jdk原始碼分析之TreeMapJDK原始碼
- 原始碼分析Kafka之Producer原始碼Kafka