Move RazorViewEngineOptionsSetup to internal namespace (#6125)
Addresses #6116
This commit is contained in:
parent
c8a1bb6914
commit
d8a95c731b
|
|
@ -0,0 +1,71 @@
|
||||||
|
// 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 Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Sets up default options for <see cref="RazorViewEngineOptions"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class RazorViewEngineOptionsSetup : IConfigureOptions<RazorViewEngineOptions>
|
||||||
|
{
|
||||||
|
private readonly IHostingEnvironment _hostingEnvironment;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of <see cref="RazorViewEngineOptions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
|
||||||
|
public RazorViewEngineOptionsSetup(IHostingEnvironment hostingEnvironment)
|
||||||
|
{
|
||||||
|
if (hostingEnvironment == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||||
|
}
|
||||||
|
|
||||||
|
_hostingEnvironment = hostingEnvironment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(RazorViewEngineOptions options)
|
||||||
|
{
|
||||||
|
if (options == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(options));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_hostingEnvironment.ContentRootFileProvider != null)
|
||||||
|
{
|
||||||
|
options.FileProviders.Add(_hostingEnvironment.ContentRootFileProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
var compilationOptions = options.CompilationOptions;
|
||||||
|
string configurationSymbol;
|
||||||
|
|
||||||
|
if (_hostingEnvironment.IsDevelopment())
|
||||||
|
{
|
||||||
|
configurationSymbol = "DEBUG";
|
||||||
|
options.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Debug);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
configurationSymbol = "RELEASE";
|
||||||
|
options.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
|
||||||
|
}
|
||||||
|
|
||||||
|
var parseOptions = options.ParseOptions;
|
||||||
|
options.ParseOptions = parseOptions.WithPreprocessorSymbols(
|
||||||
|
parseOptions.PreprocessorSymbolNames.Concat(new[] { configurationSymbol }));
|
||||||
|
|
||||||
|
options.ViewLocationFormats.Add("/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
|
||||||
|
options.ViewLocationFormats.Add("/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
||||||
|
|
||||||
|
options.AreaViewLocationFormats.Add("/Areas/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
|
||||||
|
options.AreaViewLocationFormats.Add("/Areas/{2}/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
||||||
|
options.AreaViewLocationFormats.Add("/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,64 +0,0 @@
|
||||||
// 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 Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Razor;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Sets up default options for <see cref="RazorViewEngineOptions"/>.
|
|
||||||
/// </summary>
|
|
||||||
[Obsolete("This type is for internal use and will be removed in a future version.")]
|
|
||||||
public class RazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptions>
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of <see cref="RazorViewEngineOptions"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
|
|
||||||
public RazorViewEngineOptionsSetup(
|
|
||||||
IHostingEnvironment hostingEnvironment)
|
|
||||||
: base(options => ConfigureRazor(options, hostingEnvironment))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ConfigureRazor(
|
|
||||||
RazorViewEngineOptions razorOptions,
|
|
||||||
IHostingEnvironment hostingEnvironment)
|
|
||||||
{
|
|
||||||
if (hostingEnvironment.ContentRootFileProvider != null)
|
|
||||||
{
|
|
||||||
razorOptions.FileProviders.Add(hostingEnvironment.ContentRootFileProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
var compilationOptions = razorOptions.CompilationOptions;
|
|
||||||
string configurationSymbol;
|
|
||||||
|
|
||||||
if (hostingEnvironment.IsDevelopment())
|
|
||||||
{
|
|
||||||
configurationSymbol = "DEBUG";
|
|
||||||
razorOptions.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Debug);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
configurationSymbol = "RELEASE";
|
|
||||||
razorOptions.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
|
|
||||||
}
|
|
||||||
|
|
||||||
var parseOptions = razorOptions.ParseOptions;
|
|
||||||
razorOptions.ParseOptions = parseOptions.WithPreprocessorSymbols(
|
|
||||||
parseOptions.PreprocessorSymbolNames.Concat(new[] { configurationSymbol }));
|
|
||||||
|
|
||||||
razorOptions.ViewLocationFormats.Add("/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
|
|
||||||
razorOptions.ViewLocationFormats.Add("/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
|
||||||
|
|
||||||
razorOptions.AreaViewLocationFormats.Add("/Areas/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
|
|
||||||
razorOptions.AreaViewLocationFormats.Add("/Areas/{2}/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
|
||||||
razorOptions.AreaViewLocationFormats.Add("/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,13 +2,12 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc.Razor;
|
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc
|
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
{
|
{
|
||||||
public class RazorViewEngineOptionsSetupTest
|
public class RazorViewEngineOptionsSetupTest
|
||||||
{
|
{
|
||||||
Loading…
Reference in New Issue