42 lines
1.3 KiB
C#
42 lines
1.3 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;
|
|
|
|
namespace Microsoft.AspNetCore.Razor.Evolution
|
|
{
|
|
public static class RazorEngineBuilderExtensions
|
|
{
|
|
public static IRazorEngineBuilder AddDirective(this IRazorEngineBuilder builder, DirectiveDescriptor directive)
|
|
{
|
|
if (builder == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(directive));
|
|
}
|
|
|
|
if (directive == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(directive));
|
|
}
|
|
|
|
var directiveFeature = GetDirectiveFeature(builder);
|
|
directiveFeature.Directives.Add(directive);
|
|
|
|
return builder;
|
|
}
|
|
|
|
private static IRazorDirectiveFeature GetDirectiveFeature(IRazorEngineBuilder builder)
|
|
{
|
|
var directiveFeature = builder.Features.OfType<IRazorDirectiveFeature>().FirstOrDefault();
|
|
if (directiveFeature == null)
|
|
{
|
|
directiveFeature = new DefaultRazorDirectiveFeature();
|
|
builder.Features.Add(directiveFeature);
|
|
}
|
|
|
|
return directiveFeature;
|
|
}
|
|
}
|
|
}
|