101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
// 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.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.AspNet.Mvc.Razor;
|
|
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
|
using Microsoft.AspNet.Mvc.Rendering;
|
|
using Microsoft.AspNet.Razor.TagHelpers;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
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>
|
|
/// 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="ITagHelper.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;
|
|
}
|
|
|
|
public static IMvcBuilder AddPrecompiledRazorViews(
|
|
this IMvcBuilder builder,
|
|
params Assembly[] assemblies)
|
|
{
|
|
if (builder == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(builder));
|
|
}
|
|
|
|
if (assemblies == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(assemblies));
|
|
}
|
|
|
|
builder.Services.Replace(
|
|
ServiceDescriptor.Singleton<ICompilerCacheProvider>(serviceProvider =>
|
|
ActivatorUtilities.CreateInstance<PrecompiledViewsCompilerCacheProvider>(
|
|
serviceProvider,
|
|
assemblies.AsEnumerable())));
|
|
|
|
return builder;
|
|
}
|
|
}
|
|
}
|