This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
MvcRazorMvcBuilderExtensions.cs
91 lines (80 loc) · 3.66 KB
/
MvcRazorMvcBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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 Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extensions methods for configuring MVC via an <see cref="IMvcBuilder"/>.
/// </summary>
public static class MvcRazorMvcBuilderExtensions
{
/// <summary>
/// Configures a set of <see cref="RazorViewEngineOptions"/> for the application.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="setupAction">An action to configure the <see cref="RazorViewEngineOptions"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddRazorOptions(
this IMvcBuilder builder,
Action<RazorViewEngineOptions> setupAction)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
builder.Services.Configure(setupAction);
return builder;
}
/// <summary>
/// Registers tag helpers as services and replaces the existing <see cref="ITagHelperActivator"/>
/// with an <see cref="ServiceBasedTagHelperActivator"/>.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/> instance this method extends.</param>
/// <returns>The <see cref="IMvcBuilder"/> instance this method extends.</returns>
public static IMvcBuilder AddTagHelpersAsServices(this IMvcBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
TagHelpersAsServices.AddTagHelpersAsServices(builder.PartManager, builder.Services);
return builder;
}
/// <summary>
/// Adds an initialization callback for a given <typeparamref name="TTagHelper"/>.
/// </summary>
/// <remarks>
/// The callback will be invoked on any <typeparamref name="TTagHelper"/> instance before the
/// <see cref="ITagHelperComponent.ProcessAsync(TagHelperContext, TagHelperOutput)"/> method is called.
/// </remarks>
/// <typeparam name="TTagHelper">The type of <see cref="ITagHelper"/> being initialized.</typeparam>
/// <param name="builder">The <see cref="IMvcBuilder"/> instance this method extends.</param>
/// <param name="initialize">An action to initialize the <typeparamref name="TTagHelper"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/> instance this method extends.</returns>
public static IMvcBuilder InitializeTagHelper<TTagHelper>(
this IMvcBuilder builder,
Action<TTagHelper, ViewContext> initialize)
where TTagHelper : ITagHelper
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (initialize == null)
{
throw new ArgumentNullException(nameof(initialize));
}
var initializer = new TagHelperInitializer<TTagHelper>(initialize);
builder.Services.AddSingleton(typeof(ITagHelperInitializer<TTagHelper>), initializer);
return builder;
}
}
}