Adding extension methods ConfigureMvcOptions and ConfigureRazorViewEngineOptions.

This commit is contained in:
sornaks 2015-02-03 11:13:54 -08:00
parent e805e67b4c
commit 6e8cc6ba74
9 changed files with 116 additions and 9 deletions

View File

@ -60,13 +60,13 @@ namespace MvcSample.Web
// sample's assemblies are loaded. This prevents loading controllers from other assemblies
// when the sample is used in the Functional Tests.
services.AddTransient<IAssemblyProvider, TestAssemblyProvider<Startup>>();
services.Configure<MvcOptions>(options =>
services.ConfigureMvcOptions(options =>
{
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
options.AddXmlDataContractSerializerFormatter();
options.Filters.Add(new FormatFilterAttribute());
});
services.Configure<RazorViewEngineOptions>(options =>
services.ConfigureRazorViewEngineOptions(options =>
{
var expander = new LanguageViewLocationExpander(
context => context.HttpContext.Request.Query["language"]);
@ -103,7 +103,7 @@ namespace MvcSample.Web
// when the sample is used in the Functional Tests.
services.AddTransient<IAssemblyProvider, TestAssemblyProvider<Startup>>();
services.Configure<MvcOptions>(options =>
services.ConfigureMvcOptions(options =>
{
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
options.AddXmlDataContractSerializerFormatter();

View File

@ -0,0 +1,27 @@
// Copyright (c) Microsoft Open Technologies, Inc. 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.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Razor;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Contains extension methods to <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Configures a set of <see cref="RazorViewEngineOptions"/> for the application.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="RazorViewEngineOptions"/>.</param>
public static void ConfigureRazorViewEngineOptions(
[NotNull] this IServiceCollection services,
[NotNull] Action<RazorViewEngineOptions> setupAction)
{
services.Configure(setupAction);
}
}
}

View File

@ -16,13 +16,13 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
public class MvcOptionsSetup : ConfigureOptions<MvcOptions>
{
public MvcOptionsSetup() : base(ConfigureMvc)
public MvcOptionsSetup() : base(ConfigureMvcOptions)
{
Order = DefaultOrder.DefaultFrameworkSortOrder;
}
/// <inheritdoc />
public static void ConfigureMvc(MvcOptions options)
public static void ConfigureMvcOptions(MvcOptions options)
{
// Set up ViewEngines
options.ViewEngines.Add(typeof(RazorViewEngine));

View File

@ -1,12 +1,9 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Security;
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.Framework.DependencyInjection
@ -27,6 +24,18 @@ namespace Microsoft.Framework.DependencyInjection
return services;
}
/// <summary>
/// Configures a set of <see cref="MvcOptions"/> for the application.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">The <see cref="MvcOptions"/> which need to be configured.</param>
public static void ConfigureMvcOptions(
[NotNull] this IServiceCollection services,
[NotNull] Action<MvcOptions> setupAction)
{
services.Configure(setupAction);
}
private static void ConfigureDefaultServices(IServiceCollection services, IConfiguration configuration)
{
services.AddOptions(configuration);

View File

@ -259,5 +259,21 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
var responseData = await response.Content.ReadAsStringAsync();
Assert.Contains(expectedLink, responseData, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ConfigureMvcOptionsAddsOptionsProperly()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = new HttpClient(server.CreateHandler(), false);
// Act
var response = await client.GetAsync("http://localhost/Home/GetApplicationDescription");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseData = await response.Content.ReadAsStringAsync();
Assert.Equal("This is a basic website.", responseData);
}
}
}

View File

@ -2,6 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.OptionsModel;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor
@ -18,5 +21,24 @@ namespace Microsoft.AspNet.Mvc.Razor
var ex = Assert.Throws<ArgumentNullException>(() => options.FileProvider = null);
Assert.Equal("value", ex.ParamName);
}
[Fact]
public void ConfigureRazorViewEngineOptions_ConfiguresOptionsProperly()
{
// Arrange
var services = new ServiceCollection().AddOptions();
var timeSpan = new TimeSpan(400);
// Act
services.ConfigureRazorViewEngineOptions(options => {
options.ExpirationBeforeCheckingFilesOnDisk = timeSpan;
});
var serviceProvider = services.BuildServiceProvider();
// Assert
var accessor = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
var expiration = Assert.IsType<TimeSpan>(accessor.Options.ExpirationBeforeCheckingFilesOnDisk);
Assert.Equal(timeSpan, expiration);
}
}
}

View File

@ -57,5 +57,11 @@ namespace BasicWebSite.Controllers
{
return View();
}
public string GetApplicationDescription()
{
var actionDescriptor = (ControllerActionDescriptor)ActionContext.ActionDescriptor;
return actionDescriptor.Properties["description"].ToString();
}
}
}

View File

@ -0,0 +1,22 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc.ApplicationModels;
namespace BasicWebSite
{
public class ApplicationDescription : IApplicationModelConvention
{
private string _description;
public ApplicationDescription(string description)
{
_description = description;
}
public void Apply(ApplicationModel application)
{
application.Properties["description"] = _description;
}
}
}

View File

@ -20,6 +20,11 @@ namespace BasicWebSite
services.AddMvc(configuration);
services.AddSingleton<INestedProvider<ActionDescriptorProviderContext>, ActionDescriptorCreationCounter>();
services.ConfigureMvcOptions(options =>
{
options.ApplicationModelConventions.Add(new ApplicationDescription("This is a basic website."));
});
});
// Add MVC to the request pipeline