diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs new file mode 100644 index 0000000000..9880a34570 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs @@ -0,0 +1,61 @@ +// 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.Core; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Encapsulates information that describes an . + /// + public class OutputFormatterDescriptor + { + /// + /// Creates a new instance of . + /// + /// A + /// Creates a new instance of . + /// + /// An instance of + /// that the descriptor represents. + public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter) + { + OutputFormatter = outputFormatter; + OutputFormatterType = outputFormatter.GetType(); + } + + /// + /// Gets the type of the . + /// + public Type OutputFormatterType + { + get; + private set; + } + + /// + /// Gets the instance of the . + /// + public OutputFormatter OutputFormatter + { + get; + private set; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj index 34b61069f0..031afde33d 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj +++ b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj @@ -35,6 +35,8 @@ + + diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index af5515b082..c4ad9da725 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -24,6 +24,7 @@ namespace Microsoft.AspNet.Mvc ModelBinders = new List(); ViewEngines = new List(); ValueProviderFactories = new List(); + OutputFormatters = new List(); } /// @@ -49,6 +50,8 @@ namespace Microsoft.AspNet.Mvc } } + public List OutputFormatters { get; private set; } + /// /// Provides programmatic configuration for the default . /// diff --git a/src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs new file mode 100644 index 0000000000..0781587ca6 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs @@ -0,0 +1,82 @@ +// 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 System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Extension methods for adding output formatters to a collection. + /// + public static class OutputFormatterDescriptorExtensions + { + /// + /// Adds a type representing a to a descriptor collection. + /// + /// A list of OutputFormatterDescriptors + /// Type representing an . + /// OutputFormatterDescriptor representing the added instance. + public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, + [NotNull] Type outputFormatterType) + { + var descriptor = new OutputFormatterDescriptor(outputFormatterType); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Inserts a type representing a to a descriptor collection. + /// + /// A list of OutputFormatterDescriptors + /// Type representing an . + /// OutputFormatterDescriptor representing the inserted instance. + public static OutputFormatterDescriptor Insert([NotNull] this IList descriptors, + int index, + [NotNull] Type outputFormatterType) + { + if (index < 0 || index > descriptors.Count) + { + throw new ArgumentOutOfRangeException("index"); + } + + var descriptor = new OutputFormatterDescriptor(outputFormatterType); + descriptors.Insert(index, descriptor); + return descriptor; + } + + /// + /// Adds an to a descriptor collection. + /// + /// A list of OutputFormatterDescriptors + /// An instance. + /// OutputFormatterDescriptor representing the added instance. + public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, + [NotNull] OutputFormatter outputFormatter) + { + var descriptor = new OutputFormatterDescriptor(outputFormatter); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Insert an to a descriptor collection. + /// + /// A list of OutputFormatterDescriptors + /// An instance. + /// OutputFormatterDescriptor representing the added instance. + public static OutputFormatterDescriptor Insert([NotNull] this IList descriptors, + int index, + [NotNull] OutputFormatter outputFormatter) + { + if (index < 0 || index > descriptors.Count) + { + throw new ArgumentOutOfRangeException("index"); + } + + var descriptor = new OutputFormatterDescriptor(outputFormatter); + descriptors.Insert(index, descriptor); + return descriptor; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs index 9faba693da..014d3bc92b 100644 --- a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs @@ -33,9 +33,13 @@ namespace Microsoft.AspNet.Mvc options.ModelBinders.Add(new ComplexModelDtoModelBinder()); // Set up ValueProviders - options.ValueProviderFactories.Add(new RouteValueValueProviderFactory()); + options.ValueProviderFactories.Add(new RouteValueValueProviderFactory()); options.ValueProviderFactories.Add(new QueryStringValueProviderFactory()); options.ValueProviderFactories.Add(new FormValueProviderFactory()); + + // Set up OutputFormatters + options.OutputFormatters.Add(new OutputFormatterDescriptor( + new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false))); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs new file mode 100644 index 0000000000..70987c82d7 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs @@ -0,0 +1,73 @@ +// 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. + +#if NET45 +using System; +using System.Collections.Generic; +using Moq; +using Xunit; + +namespace Microsoft.AspNet.Mvc.Core.Test +{ + public class OutputFormatterDescriptorExtensionTest + { + [Theory] + [InlineData(-1)] + [InlineData(5)] + public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) + { + // Arrange + var collection = new List + { + new OutputFormatterDescriptor(Mock.Of()), + new OutputFormatterDescriptor(Mock.Of()) + }; + + // Act & Assert + Assert.Throws("index", + () => collection.Insert(index, typeof(OutputFormatter))); + } + + [Theory] + [InlineData(-2)] + [InlineData(3)] + public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) + { + // Arrange + var collection = new List + { + new OutputFormatterDescriptor(Mock.Of()), + new OutputFormatterDescriptor(Mock.Of()) + }; + var formatter = Mock.Of(); + + // Act & Assert + Assert.Throws("index", () => collection.Insert(index, formatter)); + } + + [InlineData] + public void OutputFormatterDescriptors_AddsTypesAndInstances() + { + // Arrange + var formatter1 = Mock.Of(); + var formatter2 = Mock.Of(); + var type1 = typeof(JsonOutputFormatter); + var type2 = typeof(OutputFormatter); + var collection = new List(); + + // Act + collection.Add(formatter1); + collection.Insert(1, formatter2); + collection.Add(type1); + collection.Insert(2, type2); + + // Assert + Assert.Equal(4, collection.Count); + Assert.Equal(formatter1, collection[0].OutputFormatter); + Assert.Equal(formatter2, collection[1].OutputFormatter); + Assert.Equal(type2, collection[2].OutputFormatterType); + Assert.Equal(type1, collection[3].OutputFormatterType); + } + } +} +#endif \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs new file mode 100644 index 0000000000..c2bfc9bf22 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs @@ -0,0 +1,24 @@ +// 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.Testing; +using Xunit; + +namespace Microsoft.AspNet.Mvc.Core.Test +{ + public class OutputFormatterDescriptorTest + { + [Fact] + public void ConstructorThrows_IfTypeIsNotOutputFormatter() + { + // Arrange + var expected = "The type 'System.String' must derive from " + + "'Microsoft.AspNet.Mvc.OutputFormatter'."; + + var type = typeof(string); + + // Act & Assert + ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "outputFormatterType", expected); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj index 5490f9bc70..fd81abcdf4 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj @@ -33,6 +33,8 @@ + + diff --git a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs index d256168a7a..9942e9670f 100644 --- a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs +++ b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs @@ -60,5 +60,20 @@ namespace Microsoft.AspNet.Mvc Assert.IsType(valueProviders[1]); Assert.IsType(valueProviders[2]); } + + [Fact] + public void Setup_SetsUpOutputFormatters() + { + // Arrange + var mvcOptions = new MvcOptions(); + var setup = new MvcOptionsSetup(); + + // Act + setup.Setup(mvcOptions); + + // Assert + Assert.Equal(1, mvcOptions.OutputFormatters.Count); + Assert.IsType(mvcOptions.OutputFormatters[0].OutputFormatter); + } } } \ No newline at end of file