diff --git a/samples/MvcSample.Web/Startup.cs b/samples/MvcSample.Web/Startup.cs index 023640a31c..86be633d76 100644 --- a/samples/MvcSample.Web/Startup.cs +++ b/samples/MvcSample.Web/Startup.cs @@ -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>(); - services.Configure(options => + services.ConfigureMvcOptions(options => { options.Filters.Add(typeof(PassThroughAttribute), order: 17); options.AddXmlDataContractSerializerFormatter(); options.Filters.Add(new FormatFilterAttribute()); }); - services.Configure(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>(); - services.Configure(options => + services.ConfigureMvcOptions(options => { options.Filters.Add(typeof(PassThroughAttribute), order: 17); options.AddXmlDataContractSerializerFormatter(); diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorServiceCollectionExtensions.cs new file mode 100644 index 0000000000..f8b731f138 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorServiceCollectionExtensions.cs @@ -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 +{ + /// + /// Contains extension methods to . + /// + public static class ServiceCollectionExtensions + { + /// + /// Configures a set of for the application. + /// + /// The services available in the application. + /// An action to configure the . + public static void ConfigureRazorViewEngineOptions( + [NotNull] this IServiceCollection services, + [NotNull] Action setupAction) + { + services.Configure(setupAction); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs index 85d97c6d03..dcd3f49bd4 100644 --- a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs @@ -16,13 +16,13 @@ namespace Microsoft.AspNet.Mvc /// public class MvcOptionsSetup : ConfigureOptions { - public MvcOptionsSetup() : base(ConfigureMvc) + public MvcOptionsSetup() : base(ConfigureMvcOptions) { Order = DefaultOrder.DefaultFrameworkSortOrder; } /// - public static void ConfigureMvc(MvcOptions options) + public static void ConfigureMvcOptions(MvcOptions options) { // Set up ViewEngines options.ViewEngines.Add(typeof(RazorViewEngine)); diff --git a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs index 1788a3cb59..f77364fa6f 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs @@ -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; } + /// + /// Configures a set of for the application. + /// + /// The services available in the application. + /// The which need to be configured. + public static void ConfigureMvcOptions( + [NotNull] this IServiceCollection services, + [NotNull] Action setupAction) + { + services.Configure(setupAction); + } + private static void ConfigureDefaultServices(IServiceCollection services, IConfiguration configuration) { services.AddOptions(configuration); diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs index 1c4a28def3..bd28688ced 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs @@ -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); + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineOptionsTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineOptionsTest.cs index 1bd68c30bd..c692924a59 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineOptionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineOptionsTest.cs @@ -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(() => 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>(); + var expiration = Assert.IsType(accessor.Options.ExpirationBeforeCheckingFilesOnDisk); + Assert.Equal(timeSpan, expiration); + } } } \ No newline at end of file diff --git a/test/WebSites/BasicWebSite/Controllers/HomeController.cs b/test/WebSites/BasicWebSite/Controllers/HomeController.cs index 53b082bbcf..e59bc78c8a 100644 --- a/test/WebSites/BasicWebSite/Controllers/HomeController.cs +++ b/test/WebSites/BasicWebSite/Controllers/HomeController.cs @@ -57,5 +57,11 @@ namespace BasicWebSite.Controllers { return View(); } + + public string GetApplicationDescription() + { + var actionDescriptor = (ControllerActionDescriptor)ActionContext.ActionDescriptor; + return actionDescriptor.Properties["description"].ToString(); + } } } \ No newline at end of file diff --git a/test/WebSites/BasicWebSite/Conventions/ApplicationDescription.cs b/test/WebSites/BasicWebSite/Conventions/ApplicationDescription.cs new file mode 100644 index 0000000000..34a2bb51fb --- /dev/null +++ b/test/WebSites/BasicWebSite/Conventions/ApplicationDescription.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/test/WebSites/BasicWebSite/Startup.cs b/test/WebSites/BasicWebSite/Startup.cs index ee9a33d521..c0eb1abd21 100644 --- a/test/WebSites/BasicWebSite/Startup.cs +++ b/test/WebSites/BasicWebSite/Startup.cs @@ -20,6 +20,11 @@ namespace BasicWebSite services.AddMvc(configuration); services.AddSingleton, ActionDescriptorCreationCounter>(); + + services.ConfigureMvcOptions(options => + { + options.ApplicationModelConventions.Add(new ApplicationDescription("This is a basic website.")); + }); }); // Add MVC to the request pipeline