diff --git a/src/Microsoft.AspNet.Mvc.Core/ApplicationModelConventionExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/ApplicationModelConventionExtensions.cs new file mode 100644 index 0000000000..3ff98df64c --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ApplicationModelConventionExtensions.cs @@ -0,0 +1,93 @@ +// 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 Microsoft.AspNet.Mvc.ApplicationModels; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Contains the extension methods for . + /// + public static class ApplicationModelConventionExtensions + { + /// + /// Adds a to all the controllers in the application. + /// + /// The list of + /// in . + /// The which needs to be + /// added. + public static void Add( + [NotNull] this List conventions, + [NotNull] IControllerModelConvention controllerModelConvention) + { + conventions.Add(new ControllerApplicationModelConvention(controllerModelConvention)); + } + + /// + /// Adds a to all the actions in the application. + /// + /// The list of + /// in . + /// The which needs to be + /// added. + public static void Add( + this List conventions, + IActionModelConvention actionModelConvention) + { + conventions.Add(new ActionApplicationModelConvention(actionModelConvention)); + } + + private class ActionApplicationModelConvention : IApplicationModelConvention + { + private IActionModelConvention _actionModelConvention; + + /// + /// Initializes a new instance of . + /// + /// The action convention to be applied on all actions + /// in the application. + public ActionApplicationModelConvention([NotNull] IActionModelConvention actionModelConvention) + { + _actionModelConvention = actionModelConvention; + } + + /// + public void Apply([NotNull] ApplicationModel application) + { + foreach (var controller in application.Controllers) + { + foreach (var action in controller.Actions) + { + _actionModelConvention.Apply(action); + } + } + } + } + + private class ControllerApplicationModelConvention : IApplicationModelConvention + { + private IControllerModelConvention _controllerModelConvention; + + /// + /// Initializes a new instance of . + /// + /// The controller convention to be applied on all controllers + /// in the application. + public ControllerApplicationModelConvention([NotNull] IControllerModelConvention controllerConvention) + { + _controllerModelConvention = controllerConvention; + } + + /// + public void Apply([NotNull] ApplicationModel application) + { + foreach (var controller in application.Controllers) + { + _controllerModelConvention.Apply(controller); + } + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/IApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/IApplicationModelConvention.cs index ea746a6074..5fabf2d292 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/IApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ApplicationModels/IApplicationModelConvention.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels /// Allows customization of the of the . /// /// - /// Implementaions of this interface can be registered in + /// Implementaions of this interface can be registered in /// to customize metadata about the application. /// /// run before other types of customizations to the diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorProvider.cs index cbc7cf575d..1e6b6589cf 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerActionDescriptorProvider.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Mvc private readonly IControllerModelBuilder _applicationModelBuilder; private readonly IAssemblyProvider _assemblyProvider; private readonly IReadOnlyList _globalFilters; - private readonly IEnumerable _modelConventions; + private readonly IEnumerable _conventions; private readonly ILogger _logger; public ControllerActionDescriptorProvider([NotNull] IAssemblyProvider assemblyProvider, @@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Mvc _assemblyProvider = assemblyProvider; _applicationModelBuilder = applicationModelBuilder; _globalFilters = globalFilters.Filters; - _modelConventions = optionsAccessor.Options.ApplicationModelConventions; + _conventions = optionsAccessor.Options.Conventions; _logger = loggerFactory.Create(); } @@ -47,7 +47,7 @@ namespace Microsoft.AspNet.Mvc public IEnumerable GetDescriptors() { var applicationModel = BuildModel(); - ApplicationModelConventions.ApplyConventions(applicationModel, _modelConventions); + ApplicationModelConventions.ApplyConventions(applicationModel, _conventions); if (_logger.IsEnabled(LogLevel.Verbose)) { foreach (var controller in applicationModel.Controllers) diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index 84588dadb0..3e84d7cc12 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc public MvcOptions() { - ApplicationModelConventions = new List(); + Conventions = new List(); ModelBinders = new List(); ViewEngines = new List(); ValueProviderFactories = new List(); @@ -133,7 +133,7 @@ namespace Microsoft.AspNet.Mvc /// Gets a list of instances that will be applied to /// the when discovering actions. /// - public List ApplicationModelConventions { get; private set; } + public List Conventions { get; private set; } /// /// Gets or sets the flag which causes content negotiation to ignore Accept header diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs index 0f7b36016a..daefb57eb4 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs @@ -8,7 +8,7 @@ using Microsoft.AspNet.Mvc.ApplicationModels; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { - public class WebApiActionConventionsApplicationModelConvention : IApplicationModelConvention + public class WebApiActionConventionsApplicationModelConvention : IControllerModelConvention { private static readonly string[] SupportedHttpMethodConventions = new string[] { @@ -21,13 +21,31 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim "OPTIONS", }; - public void Apply(ApplicationModel application) + public void Apply(ControllerModel controller) { - foreach (var controller in application.Controllers) + if (IsConventionApplicable(controller)) { - if (IsConventionApplicable(controller)) + var newActions = new List(); + + foreach (var action in controller.Actions) { - Apply(controller); + SetHttpMethodFromConvention(action); + + // Action Name doesn't really come into play with attribute routed actions. However for a + // non-attribute-routed action we need to create a 'named' version and an 'unnamed' version. + if (!IsActionAttributeRouted(action)) + { + var namedAction = action; + + var unnamedAction = new ActionModel(namedAction); + unnamedAction.RouteConstraints.Add(new UnnamedActionRouteConstraint()); + newActions.Add(unnamedAction); + } + } + + foreach (var action in newActions) + { + controller.Actions.Add(action); } } } @@ -37,32 +55,6 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim return controller.Attributes.OfType().Any(); } - private void Apply(ControllerModel controller) - { - var newActions = new List(); - - foreach (var action in controller.Actions) - { - SetHttpMethodFromConvention(action); - - // Action Name doesn't really come into play with attribute routed actions. However for a - // non-attribute-routed action we need to create a 'named' version and an 'unnamed' version. - if (!IsActionAttributeRouted(action)) - { - var namedAction = action; - - var unnamedAction = new ActionModel(namedAction); - unnamedAction.RouteConstraints.Add(new UnnamedActionRouteConstraint()); - newActions.Add(unnamedAction); - } - } - - foreach (var action in newActions) - { - controller.Actions.Add(action); - } - } - private bool IsActionAttributeRouted(ActionModel action) { if (action.Controller.AttributeRoutes.Count > 0) diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs index 6157100307..1f85d5f464 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs @@ -6,16 +6,13 @@ using Microsoft.AspNet.Mvc.ApplicationModels; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { - public class WebApiOverloadingApplicationModelConvention : IApplicationModelConvention + public class WebApiOverloadingApplicationModelConvention : IActionModelConvention { - public void Apply(ApplicationModel application) + public void Apply(ActionModel action) { - foreach (var controller in application.Controllers) + if (IsConventionApplicable(action.Controller)) { - if (IsConventionApplicable(controller)) - { - Apply(controller); - } + action.ActionConstraints.Add(new OverloadActionConstraint()); } } @@ -23,13 +20,5 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim { return controller.Attributes.OfType().Any(); } - - private void Apply(ControllerModel controller) - { - foreach (var action in controller.Actions) - { - action.ActionConstraints.Add(new OverloadActionConstraint()); - } - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs index 117c7a85d1..2e6631c2e4 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs @@ -9,27 +9,11 @@ using Microsoft.AspNet.Mvc.ModelBinding; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { - public class WebApiParameterConventionsApplicationModelConvention : IApplicationModelConvention + public class WebApiParameterConventionsApplicationModelConvention : IActionModelConvention { - public void Apply(ApplicationModel application) + public void Apply(ActionModel action) { - foreach (var controller in application.Controllers) - { - if (IsConventionApplicable(controller)) - { - Apply(controller); - } - } - } - - private bool IsConventionApplicable(ControllerModel controller) - { - return controller.Attributes.OfType().Any(); - } - - private void Apply(ControllerModel controller) - { - foreach (var action in controller.Actions) + if (IsConventionApplicable(action.Controller)) { foreach (var parameter in action.Parameters) { @@ -57,5 +41,10 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim } } } + + private bool IsConventionApplicable(ControllerModel controller) + { + return controller.Attributes.OfType().Any(); + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs index a01d91b9c3..61cbd7ab9a 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs @@ -6,7 +6,7 @@ using Microsoft.AspNet.Mvc.ApplicationModels; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { - public class WebApiRoutesApplicationModelConvention : IApplicationModelConvention + public class WebApiRoutesApplicationModelConvention : IControllerModelConvention { private readonly string _area; @@ -15,14 +15,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim _area = area; } - public void Apply(ApplicationModel application) + public void Apply(ControllerModel controller) { - foreach (var controller in application.Controllers) + if (IsConventionApplicable(controller)) { - if (IsConventionApplicable(controller)) - { - Apply(controller); - } + controller.RouteConstraints.Add(new AreaAttribute(_area)); } } @@ -30,10 +27,5 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim { return controller.Attributes.OfType().Any(); } - - private void Apply(ControllerModel controller) - { - controller.RouteConstraints.Add(new AreaAttribute(_area)); - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs index e4f774386f..83fba7b4e9 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs @@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim public void Configure(MvcOptions options, string name = "") { // Add webapi behaviors to controllers with the appropriate attributes - options.ApplicationModelConventions.Add(new WebApiActionConventionsApplicationModelConvention()); - options.ApplicationModelConventions.Add(new WebApiParameterConventionsApplicationModelConvention()); - options.ApplicationModelConventions.Add(new WebApiOverloadingApplicationModelConvention()); - options.ApplicationModelConventions.Add(new WebApiRoutesApplicationModelConvention(area: DefaultAreaName)); + options.Conventions.Add(new WebApiActionConventionsApplicationModelConvention()); + options.Conventions.Add(new WebApiParameterConventionsApplicationModelConvention()); + options.Conventions.Add(new WebApiOverloadingApplicationModelConvention()); + options.Conventions.Add(new WebApiRoutesApplicationModelConvention(area: DefaultAreaName)); // Add an action filter for handling the HttpResponseException. options.Filters.Add(new HttpResponseExceptionActionFilter()); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ActionApplicationModelConventionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ActionApplicationModelConventionTest.cs new file mode 100644 index 0000000000..bc1cb2e5d4 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ActionApplicationModelConventionTest.cs @@ -0,0 +1,59 @@ +// 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.Reflection; +using Xunit; + +namespace Microsoft.AspNet.Mvc.ApplicationModels +{ + public class ActionApplicationModelConventionTest + { + [Fact] + public void DefaultActionModelConvention_AppliesToAllActionsInApp() + { + // Arrange + var options = new MvcOptions(); + var app = new ApplicationModel(); + app.Controllers.Add(new ControllerModel(typeof(HelloController).GetTypeInfo(), new List())); + app.Controllers.Add(new ControllerModel(typeof(WorldController).GetTypeInfo(), new List())); + options.Conventions.Add(new SimpleActionConvention()); + + // Act + options.Conventions[0].Apply(app); + + // Assert + foreach (var controller in app.Controllers) + { + foreach (var action in controller.Actions) + { + Assert.True(action.Properties.ContainsKey("TestProperty")); + } + } + } + + private class HelloController : Controller + { + public string GetHello() + { + return "Hello"; + } + } + + private class WorldController : Controller + { + public string GetWorld() + { + return "World!"; + } + } + + private class SimpleActionConvention : IActionModelConvention + { + public void Apply([NotNull] ActionModel action) + { + action.Properties.Add("TestProperty", "TestValue"); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ControllerApplicationModelConventionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ControllerApplicationModelConventionTest.cs new file mode 100644 index 0000000000..a7b893436d --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ApplicationModel/ControllerApplicationModelConventionTest.cs @@ -0,0 +1,44 @@ +// 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.Reflection; +using Microsoft.AspNet.Mvc; +using Xunit; + +namespace Microsoft.AspNet.Mvc.ApplicationModels +{ + public class ControllerApplicationModelConventionTest + { + [Fact] + public void DefaultControllerModelConvention_AppliesToAllControllers() + { + // Arrange + var options = new MvcOptions(); + var app = new ApplicationModel(); + app.Controllers.Add(new ControllerModel(typeof(HelloController).GetTypeInfo(), new List())); + app.Controllers.Add(new ControllerModel(typeof(WorldController).GetTypeInfo(), new List())); + options.Conventions.Add(new SimpleControllerConvention()); + + // Act + options.Conventions[0].Apply(app); + + // Assert + foreach (var controller in app.Controllers) + { + Assert.True(controller.Properties.ContainsKey("TestProperty")); + } + } + + private class HelloController : Controller { } + private class WorldController : Controller { } + + private class SimpleControllerConvention : IControllerModelConvention + { + public void Apply([NotNull] ControllerModel controller) + { + controller.Properties.Add("TestProperty", "TestValue"); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorProviderTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorProviderTests.cs index b6be78d158..9459fae59d 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorProviderTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionDescriptorProviderTests.cs @@ -1187,7 +1187,7 @@ namespace Microsoft.AspNet.Mvc.Test .Callback(() => { Assert.Equal(3, sequence++); }); var options = new MockMvcOptionsAccessor(); - options.Options.ApplicationModelConventions.Add(applicationConvention.Object); + options.Options.Conventions.Add(applicationConvention.Object); var applicationModel = new ApplicationModel(); @@ -1209,7 +1209,7 @@ namespace Microsoft.AspNet.Mvc.Test actionModel.Parameters.Add(parameterModel); // Act - ApplicationModelConventions.ApplyConventions(applicationModel, options.Options.ApplicationModelConventions); + ApplicationModelConventions.ApplyConventions(applicationModel, options.Options.Conventions); // Assert Assert.Equal(4, sequence); @@ -1411,7 +1411,7 @@ namespace Microsoft.AspNet.Mvc.Test .Returns(new Assembly[] { type.Assembly }); var options = new MockMvcOptionsAccessor(); - options.Options.ApplicationModelConventions.Add(convention); + options.Options.Conventions.Add(convention); return new ControllerActionDescriptorProvider( assemblyProvider.Object, diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ApplicationModelTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ApplicationModelTest.cs index 2a01b52a6d..232dfc9c6c 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ApplicationModelTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/ApplicationModelTest.cs @@ -3,6 +3,7 @@ using System; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.TestHost; @@ -116,5 +117,44 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var body = await response.Content.ReadAsStringAsync(); Assert.Equal("Specific Action Description", body); } + + [Fact] + public async Task ApplicationModelExtensions_AddsConventionToAllControllers() + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + + // Act + var response = await client.GetAsync("http://localhost/Lisence/GetLisence"); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var body = await response.Content.ReadAsStringAsync(); + Assert.Equal("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.", body); + } + + [Fact] + public async Task ApplicationModelExtensions_AddsConventionToAllActions() + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/GetHelloWorld"); + request.Headers.Add("helloWorld", "HelloWorld"); + + // Act + var response = await client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var body = await response.Content.ReadAsStringAsync(); + Assert.Equal("From Header - HelloWorld", body); + } } } \ No newline at end of file diff --git a/test/WebSites/ApiExplorerWebSite/Startup.cs b/test/WebSites/ApiExplorerWebSite/Startup.cs index f0ece731ea..0ba6f16d57 100644 --- a/test/WebSites/ApiExplorerWebSite/Startup.cs +++ b/test/WebSites/ApiExplorerWebSite/Startup.cs @@ -23,8 +23,8 @@ namespace ApiExplorerWebSite { options.Filters.AddService(typeof(ApiExplorerDataFilter)); - options.ApplicationModelConventions.Add(new ApiExplorerVisibilityEnabledConvention()); - options.ApplicationModelConventions.Add(new ApiExplorerVisibilityDisabledConvention( + options.Conventions.Add(new ApiExplorerVisibilityEnabledConvention()); + options.Conventions.Add(new ApiExplorerVisibilityDisabledConvention( typeof(ApiExplorerVisbilityDisabledByConventionController))); options.OutputFormatters.Clear(); diff --git a/test/WebSites/ApplicationModelWebSite/Controllers/HomeController.cs b/test/WebSites/ApplicationModelWebSite/Controllers/HomeController.cs index 5f56ab7b0c..539af04b9e 100644 --- a/test/WebSites/ApplicationModelWebSite/Controllers/HomeController.cs +++ b/test/WebSites/ApplicationModelWebSite/Controllers/HomeController.cs @@ -12,5 +12,12 @@ namespace ApplicationModelWebSite var actionDescriptor = (ControllerActionDescriptor)ActionContext.ActionDescriptor; return actionDescriptor.Properties["description"].ToString(); } + + [HttpGet("Home/GetHelloWorld")] + public object GetHelloWorld([FromHeader] string helloWorld) + { + var actionDescriptor = (ControllerActionDescriptor)ActionContext.ActionDescriptor; + return actionDescriptor.Properties["source"].ToString() + " - " + helloWorld; + } } } \ No newline at end of file diff --git a/test/WebSites/ApplicationModelWebSite/Controllers/LisenceController.cs b/test/WebSites/ApplicationModelWebSite/Controllers/LisenceController.cs new file mode 100644 index 0000000000..a9fa4bdacf --- /dev/null +++ b/test/WebSites/ApplicationModelWebSite/Controllers/LisenceController.cs @@ -0,0 +1,17 @@ +// 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; + +namespace ApplicationModelWebSite.Controllers +{ + public class LisenceController : Controller + { + [HttpGet("Lisence/GetLisence")] + public string GetLisence() + { + var actionDescriptor = (ControllerActionDescriptor)ActionContext.ActionDescriptor; + return actionDescriptor.Properties["lisence"].ToString(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/ApplicationModelWebSite/Conventions/ControllerLisenceConvention.cs b/test/WebSites/ApplicationModelWebSite/Conventions/ControllerLisenceConvention.cs new file mode 100644 index 0000000000..b25c043448 --- /dev/null +++ b/test/WebSites/ApplicationModelWebSite/Conventions/ControllerLisenceConvention.cs @@ -0,0 +1,17 @@ +// 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 ApplicationModelWebSite +{ + public class ControllerLisenceConvention : IControllerModelConvention + { + public void Apply(ControllerModel controller) + { + controller.Properties["lisence"] = "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."; + } + } +} \ No newline at end of file diff --git a/test/WebSites/ApplicationModelWebSite/Conventions/FromHeaderConvention.cs b/test/WebSites/ApplicationModelWebSite/Conventions/FromHeaderConvention.cs new file mode 100644 index 0000000000..f6c2e7687e --- /dev/null +++ b/test/WebSites/ApplicationModelWebSite/Conventions/FromHeaderConvention.cs @@ -0,0 +1,23 @@ +// 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.Linq; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.ApplicationModels; + +namespace ApplicationModelWebSite +{ + public class FromHeaderConvention : IActionModelConvention + { + public void Apply(ActionModel action) + { + foreach (var param in action.Parameters) + { + if (param.Attributes.Any(p => p.GetType() == typeof(FromHeaderAttribute))) + { + param.Action.Properties["source"] = "From Header"; + } + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/ApplicationModelWebSite/Startup.cs b/test/WebSites/ApplicationModelWebSite/Startup.cs index d390aa5459..030dc2da71 100644 --- a/test/WebSites/ApplicationModelWebSite/Startup.cs +++ b/test/WebSites/ApplicationModelWebSite/Startup.cs @@ -19,7 +19,9 @@ namespace ApplicationModelWebSite services.Configure(options => { - options.ApplicationModelConventions.Add(new ApplicationDescription("Common Application Description")); + options.Conventions.Add(new ApplicationDescription("Common Application Description")); + options.Conventions.Add(new ControllerLisenceConvention()); + options.Conventions.Add(new FromHeaderConvention()); }); }); diff --git a/test/WebSites/BasicWebSite/Startup.cs b/test/WebSites/BasicWebSite/Startup.cs index c0eb1abd21..7c9eda58fd 100644 --- a/test/WebSites/BasicWebSite/Startup.cs +++ b/test/WebSites/BasicWebSite/Startup.cs @@ -23,7 +23,7 @@ namespace BasicWebSite services.ConfigureMvcOptions(options => { - options.ApplicationModelConventions.Add(new ApplicationDescription("This is a basic website.")); + options.Conventions.Add(new ApplicationDescription("This is a basic website.")); }); });