// 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
{
///
/// Extensions methods for configuring MVC via an .
///
public static class MvcRazorMvcBuilderExtensions
{
///
/// Configures a set of for the application.
///
/// The .
/// An action to configure the .
/// The .
public static IMvcBuilder AddRazorOptions(
this IMvcBuilder builder,
Action setupAction)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
builder.Services.Configure(setupAction);
return builder;
}
///
/// Adds an initialization callback for a given .
///
///
/// The callback will be invoked on any instance before the
/// method is called.
///
/// The type of being initialized.
/// The instance this method extends.
/// An action to initialize the .
/// The instance this method extends.
public static IMvcBuilder InitializeTagHelper(
this IMvcBuilder builder,
Action initialize)
where TTagHelper : ITagHelper
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (initialize == null)
{
throw new ArgumentNullException(nameof(initialize));
}
var initializer = new TagHelperInitializer(initialize);
builder.Services.AddSingleton(typeof(ITagHelperInitializer), 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(serviceProvider =>
ActivatorUtilities.CreateInstance(
serviceProvider,
assemblies.AsEnumerable())));
return builder;
}
}
}