diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/StringOutputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/StringOutputFormatter.cs index 5e417f420f..ca9abc9e9c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/StringOutputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/StringOutputFormatter.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { /// - /// Always writes a string value to the response, regardless of requested content type. + /// A for simple text content. /// public class StringOutputFormatter : TextOutputFormatter { @@ -29,18 +29,10 @@ public override bool CanWriteResult(OutputFormatterCanWriteContext context) throw new ArgumentNullException(nameof(context)); } - // Ignore the passed in content type, if the object is string - // always return it as a text/plain format. if (context.ObjectType == typeof(string) || context.Object is string) { - if (!context.ContentType.HasValue) - { - var mediaType = SupportedMediaTypes[0]; - var encoding = SupportedEncodings[0]; - context.ContentType = new StringSegment(MediaType.ReplaceEncoding(mediaType, encoding)); - } - - return true; + // Call into base to check if the current request's content type is a supported media type. + return base.CanWriteResult(context); } return false; diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/StringOutputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/StringOutputFormatterTests.cs index 5f14fb2d60..9069aa06f6 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/StringOutputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/StringOutputFormatterTests.cs @@ -12,39 +12,52 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { - public class TextPlainFormatterTests + public class StringOutputFormatterTests { - public static IEnumerable OutputFormatterContextValues + public static IEnumerable CanWriteStringsData { get { - // object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult - yield return new object[] { "valid value", true, true }; - yield return new object[] { null, true, true }; - yield return new object[] { null, false, false }; - yield return new object[] { new object(), false, false }; + // object value, bool useDeclaredTypeAsString + yield return new object[] { "declared and runtime type are same", true }; + yield return new object[] { "declared and runtime type are different", false }; + yield return new object[] { null, true }; } } - [Fact] - public void CanWriteResult_SetsAcceptContentType() + public static TheoryData CannotWriteNonStringsData + { + get + { + return new TheoryData() + { + null, + new object() + }; + } + } + + [Theory] + [InlineData("application/json")] + [InlineData("application/xml")] + public void CannotWriteUnsupportedMediaType(string contentType) { // Arrange var formatter = new StringOutputFormatter(); - var expectedContentType = new StringSegment("application/json"); + var expectedContentType = new StringSegment(contentType); var context = new OutputFormatterWriteContext( new DefaultHttpContext(), new TestHttpResponseStreamWriterFactory().CreateWriter, typeof(string), "Thisisastring"); - context.ContentType = expectedContentType; + context.ContentType = new StringSegment(contentType); // Act var result = formatter.CanWriteResult(context); // Assert - Assert.True(result); + Assert.False(result); Assert.Equal(expectedContentType, context.ContentType); } @@ -53,7 +66,6 @@ public void CanWriteResult_DefaultContentType() { // Arrange var formatter = new StringOutputFormatter(); - var context = new OutputFormatterWriteContext( new DefaultHttpContext(), new TestHttpResponseStreamWriterFactory().CreateWriter, @@ -65,18 +77,17 @@ public void CanWriteResult_DefaultContentType() // Assert Assert.True(result); - Assert.Equal(new StringSegment("text/plain; charset=utf-8"), context.ContentType); + Assert.Equal(new StringSegment("text/plain"), context.ContentType); } [Theory] - [MemberData(nameof(OutputFormatterContextValues))] - public void CanWriteResult_ReturnsTrueForStringTypes( + [MemberData(nameof(CanWriteStringsData))] + public void CanWriteStrings( object value, - bool useDeclaredTypeAsString, - bool expectedCanWriteResult) + bool useDeclaredTypeAsString) { // Arrange - var expectedContentType = new StringSegment("application/json"); + var expectedContentType = new StringSegment("text/plain"); var formatter = new StringOutputFormatter(); var type = useDeclaredTypeAsString ? typeof(string) : typeof(object); @@ -86,13 +97,35 @@ public void CanWriteResult_ReturnsTrueForStringTypes( new TestHttpResponseStreamWriterFactory().CreateWriter, type, value); - context.ContentType = expectedContentType; + context.ContentType = new StringSegment("text/plain"); + + // Act + var result = formatter.CanWriteResult(context); + + // Assert + Assert.True(result); + Assert.Equal(expectedContentType, context.ContentType); + } + + [Theory] + [MemberData(nameof(CannotWriteNonStringsData))] + public void CannotWriteNonStrings(object value) + { + // Arrange + var expectedContentType = new StringSegment("text/plain"); + var formatter = new StringOutputFormatter(); + var context = new OutputFormatterWriteContext( + new DefaultHttpContext(), + new TestHttpResponseStreamWriterFactory().CreateWriter, + typeof(object), + value); + context.ContentType = new StringSegment("text/plain"); // Act var result = formatter.CanWriteResult(context); // Assert - Assert.Equal(expectedCanWriteResult, result); + Assert.False(result); Assert.Equal(expectedContentType, context.ContentType); } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs index 41cd535e49..746e0c2b6d 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs @@ -345,33 +345,31 @@ public async Task XmlFormatter_SupportedMediaType_DoesNotChangeAcrossRequests() } [Theory] - [InlineData(true)] - [InlineData(false)] - public async Task ObjectResult_WithStringReturnType_DefaultToTextPlain(bool matchFormatterOnObjectType) + [InlineData(null)] + [InlineData("text/plain")] + [InlineData("text/plain; charset=utf-8")] + [InlineData("text/html, application/xhtml+xml, image/jxr, */*")] // typical browser accept header + public async Task ObjectResult_WithStringReturnType_DefaultToTextPlain(string acceptMediaType) { // Arrange - var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=true" + - matchFormatterOnObjectType; - var request = new HttpRequestMessage(HttpMethod.Get, targetUri); + var request = new HttpRequestMessage(HttpMethod.Get, "FallbackOnTypeBasedMatch/ReturnString"); + request.Headers.Accept.ParseAdd(acceptMediaType); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType); + Assert.Equal("text/plain; charset=utf-8", response.Content.Headers.ContentType.ToString()); var actualBody = await response.Content.ReadAsStringAsync(); Assert.Equal("Hello World!", actualBody); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public async Task ObjectResult_WithStringReturnType_SetsMediaTypeToAccept(bool matchFormatterOnObjectType) + [Fact] + public async Task ObjectResult_WithStringReturnType_AndNonTextPlainMediaType_DoesNotReturnTextPlain() { // Arrange - var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=" + - matchFormatterOnObjectType; + var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString"; var request = new HttpRequestMessage(HttpMethod.Get, targetUri); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); @@ -380,9 +378,9 @@ public async Task ObjectResult_WithStringReturnType_SetsMediaTypeToAccept(bool m // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType); + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); var actualBody = await response.Content.ReadAsStringAsync(); - Assert.Equal("Hello World!", actualBody); + Assert.Equal("\"Hello World!\"", actualBody); } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs new file mode 100644 index 0000000000..b486b66009 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs @@ -0,0 +1,142 @@ +// 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.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters.Xml; +using Microsoft.AspNetCore.Testing.xunit; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests +{ + /// + /// These tests are for scenarios when is False, which is the default. + /// + public class DoNotRespectBrowserAcceptHeaderTests : IClassFixture> + { + public DoNotRespectBrowserAcceptHeaderTests(MvcTestFixture fixture) + { + Client = fixture.Client; + } + + public HttpClient Client { get; } + + [Theory] + [InlineData("application/xml,*/*;q=0.2")] + [InlineData("application/xml,*/*")] + public async Task AllMediaRangeAcceptHeader_FirstFormatterInListWritesResponse(string acceptHeader) + { + // Arrange + var request = RequestWithAccept("http://localhost/DoNotRespectBrowserAcceptHeader/EmployeeInfo", acceptHeader); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotNull(response.Content.Headers.ContentType); + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); + var responseData = await response.Content.ReadAsStringAsync(); + Assert.Equal("{\"id\":10,\"name\":\"John\"}", responseData); + } + + [ConditionalTheory] + // Mono issue - https://github.com/aspnet/External/issues/18 + [FrameworkSkipCondition(RuntimeFrameworks.Mono)] + [InlineData("application/xml,*/*;q=0.2")] + [InlineData("application/xml,*/*")] + public async Task AllMediaRangeAcceptHeader_ProducesAttributeIsHonored(string acceptHeader) + { + // Arrange + var request = RequestWithAccept( + "http://localhost/DoNotRespectBrowserAcceptHeader/EmployeeInfoWithProduces", + acceptHeader); + var expectedResponseData = + "20Mike" + + ""; + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotNull(response.Content.Headers.ContentType); + Assert.Equal("application/xml; charset=utf-8", response.Content.Headers.ContentType.ToString()); + var responseData = await response.Content.ReadAsStringAsync(); + XmlAssert.Equal(expectedResponseData, responseData); + } + + [ConditionalTheory] + // Mono issue - https://github.com/aspnet/External/issues/18 + [FrameworkSkipCondition(RuntimeFrameworks.Mono)] + [InlineData("application/xml,*/*;q=0.2")] + [InlineData("application/xml,*/*")] + public async Task AllMediaRangeAcceptHeader_WithContentTypeHeader_ContentTypeIsIgnored(string acceptHeader) + { + // Arrange + var requestData = + "35Jimmy" + + ""; + var expectedResponseData = @"{""id"":35,""name"":""Jimmy""}"; + var request = RequestWithAccept("http://localhost/DoNotRespectBrowserAcceptHeader/CreateEmployee", acceptHeader); + request.Content = new StringContent(requestData, Encoding.UTF8, "application/xml"); + request.Method = HttpMethod.Post; + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotNull(response.Content.Headers.ContentType); + + // Site uses default output formatter (ignores Accept header) because that header contained a wildcard match. + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); + + var responseData = await response.Content.ReadAsStringAsync(); + Assert.Equal(expectedResponseData, responseData); + } + + [ConditionalTheory] + // Mono issue - https://github.com/aspnet/External/issues/18 + [FrameworkSkipCondition(RuntimeFrameworks.Mono)] + [InlineData("application/xml,application/json;q=0.2")] + [InlineData("application/xml,application/json")] + public async Task AllMediaRangeAcceptHeader_WithExactMatch_ReturnsExpectedContent(string acceptHeader) + { + // Arrange + var requestData = + "35Jimmy" + + ""; + var request = RequestWithAccept("http://localhost/DoNotRespectBrowserAcceptHeader/CreateEmployee", acceptHeader); + request.Content = new StringContent(requestData, Encoding.UTF8, "application/xml"); + request.Method = HttpMethod.Post; + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotNull(response.Content.Headers.ContentType); + Assert.Equal("application/xml; charset=utf-8", response.Content.Headers.ContentType.ToString()); + var responseData = await response.Content.ReadAsStringAsync(); + Assert.Equal(requestData, responseData); + } + + private static HttpRequestMessage RequestWithAccept(string url, string accept) + { + var request = new HttpRequestMessage(HttpMethod.Get, url); + request.Headers.Add("Accept", accept); + + return request; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs index 6080d661d4..035aef5a30 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs @@ -1,32 +1,31 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.Net; using System.Net.Http; -using System.Text; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Formatters.Xml; -using Microsoft.AspNetCore.Testing.xunit; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { - public class RespectBrowserAcceptHeaderTests : IClassFixture> + /// + /// These tests are for scenarios when is True(default is False). + /// + public class RespectBrowserAcceptHeaderTests : IClassFixture> { - public RespectBrowserAcceptHeaderTests(MvcTestFixture fixture) + public RespectBrowserAcceptHeaderTests(MvcTestFixture fixture) { Client = fixture.Client; } public HttpClient Client { get; } - [Theory] - [InlineData("application/xml,*/*;q=0.2")] - [InlineData("application/xml,*/*")] - public async Task AllMediaRangeAcceptHeader_FirstFormatterInListWritesResponse(string acceptHeader) + [Fact] + public async Task ReturnStringFromAction_StringOutputFormatterDoesNotWriteTheResponse() { // Arrange - var request = RequestWithAccept("http://localhost/RespectBrowserAcceptHeader/EmployeeInfo", acceptHeader); + var request = new HttpRequestMessage(HttpMethod.Get, "RespectBrowserAcceptHeader/ReturnString"); + request.Headers.Accept.ParseAdd("text/html, application/json, image/jpeg, */*; q=.2"); // Act var response = await Client.SendAsync(request); @@ -37,24 +36,15 @@ public async Task AllMediaRangeAcceptHeader_FirstFormatterInListWritesResponse(s Assert.NotNull(response.Content.Headers.ContentType); Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); var responseData = await response.Content.ReadAsStringAsync(); - Assert.Equal("{\"id\":10,\"name\":\"John\"}", responseData); + Assert.Equal("\"Hello World!\"", responseData); } - [ConditionalTheory] - // Mono issue - https://github.com/aspnet/External/issues/18 - [FrameworkSkipCondition(RuntimeFrameworks.Mono)] - [InlineData("application/xml,*/*;q=0.2")] - [InlineData("application/xml,*/*")] - public async Task AllMediaRangeAcceptHeader_ProducesAttributeIsHonored(string acceptHeader) + [Fact] + public async Task ReturnStringFromAction_AcceptHeaderWithTextPlain_WritesTextPlainResponse() { // Arrange - var request = RequestWithAccept( - "http://localhost/RespectBrowserAcceptHeader/EmployeeInfoWithProduces", - acceptHeader); - var expectedResponseData = - "20Mike" + - ""; + var request = new HttpRequestMessage(HttpMethod.Get, "RespectBrowserAcceptHeader/ReturnString"); + request.Headers.Accept.ParseAdd("text/plain; charset=utf-8"); // Act var response = await Client.SendAsync(request); @@ -63,77 +53,9 @@ public async Task AllMediaRangeAcceptHeader_ProducesAttributeIsHonored(string ac Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotNull(response.Content.Headers.ContentType); - Assert.Equal("application/xml; charset=utf-8", response.Content.Headers.ContentType.ToString()); + Assert.Equal("text/plain; charset=utf-8", response.Content.Headers.ContentType.ToString()); var responseData = await response.Content.ReadAsStringAsync(); - XmlAssert.Equal(expectedResponseData, responseData); - } - - [ConditionalTheory] - // Mono issue - https://github.com/aspnet/External/issues/18 - [FrameworkSkipCondition(RuntimeFrameworks.Mono)] - [InlineData("application/xml,*/*;q=0.2")] - [InlineData("application/xml,*/*")] - public async Task AllMediaRangeAcceptHeader_WithContentTypeHeader_ContentTypeIsIgnored(string acceptHeader) - { - // Arrange - var requestData = - "35Jimmy" + - ""; - var expectedResponseData = @"{""id"":35,""name"":""Jimmy""}"; - var request = RequestWithAccept("http://localhost/RespectBrowserAcceptHeader/CreateEmployee", acceptHeader); - request.Content = new StringContent(requestData, Encoding.UTF8, "application/xml"); - request.Method = HttpMethod.Post; - - // Act - var response = await Client.SendAsync(request); - - // Assert - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.NotNull(response.Content); - Assert.NotNull(response.Content.Headers.ContentType); - - // Site uses default output formatter (ignores Accept header) because that header contained a wildcard match. - Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); - - var responseData = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedResponseData, responseData); - } - - [ConditionalTheory] - // Mono issue - https://github.com/aspnet/External/issues/18 - [FrameworkSkipCondition(RuntimeFrameworks.Mono)] - [InlineData("application/xml,application/json;q=0.2")] - [InlineData("application/xml,application/json")] - public async Task AllMediaRangeAcceptHeader_WithExactMatch_ReturnsExpectedContent(string acceptHeader) - { - // Arrange - var requestData = - "35Jimmy" + - ""; - var request = RequestWithAccept("http://localhost/RespectBrowserAcceptHeader/CreateEmployee", acceptHeader); - request.Content = new StringContent(requestData, Encoding.UTF8, "application/xml"); - request.Method = HttpMethod.Post; - - // Act - var response = await Client.SendAsync(request); - - // Assert - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.NotNull(response.Content); - Assert.NotNull(response.Content.Headers.ContentType); - Assert.Equal("application/xml; charset=utf-8", response.Content.Headers.ContentType.ToString()); - var responseData = await response.Content.ReadAsStringAsync(); - Assert.Equal(requestData, responseData); - } - - private static HttpRequestMessage RequestWithAccept(string url, string accept) - { - var request = new HttpRequestMessage(HttpMethod.Get, url); - request.Headers.Add("Accept", accept); - - return request; + Assert.Equal("Hello World!", responseData); } } } \ No newline at end of file diff --git a/test/WebSites/FormatterWebSite/Controllers/DoNotRespectBrowserAcceptHeaderController.cs b/test/WebSites/FormatterWebSite/Controllers/DoNotRespectBrowserAcceptHeaderController.cs new file mode 100644 index 0000000000..1684348e27 --- /dev/null +++ b/test/WebSites/FormatterWebSite/Controllers/DoNotRespectBrowserAcceptHeaderController.cs @@ -0,0 +1,49 @@ +// 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 Microsoft.AspNetCore.Mvc; + +namespace FormatterWebSite.Controllers +{ + public class DoNotRespectBrowserAcceptHeaderController : Controller + { + [HttpGet] + public Employee EmployeeInfo() + { + return new Employee() + { + Id = 10, + Name = "John" + }; + } + + [HttpGet] + [Produces("application/xml")] + public Employee EmployeeInfoWithProduces() + { + return new Employee() + { + Id = 20, + Name = "Mike" + }; + } + + [HttpPost] + public IActionResult CreateEmployee([FromBody]Employee employee) + { + if(!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + return new ObjectResult(employee); + } + + public class Employee + { + public int Id { get; set; } + + public string Name { get; set; } + } + } +} diff --git a/test/WebSites/FormatterWebSite/Controllers/RespectBrowserAcceptHeaderController.cs b/test/WebSites/FormatterWebSite/Controllers/RespectBrowserAcceptHeaderController.cs index 959b3ec569..d5b95b30d4 100644 --- a/test/WebSites/FormatterWebSite/Controllers/RespectBrowserAcceptHeaderController.cs +++ b/test/WebSites/FormatterWebSite/Controllers/RespectBrowserAcceptHeaderController.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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 Microsoft.AspNetCore.Mvc; @@ -8,42 +8,9 @@ namespace FormatterWebSite.Controllers public class RespectBrowserAcceptHeaderController : Controller { [HttpGet] - public Employee EmployeeInfo() + public string ReturnString() { - return new Employee() - { - Id = 10, - Name = "John" - }; - } - - [HttpGet] - [Produces("application/xml")] - public Employee EmployeeInfoWithProduces() - { - return new Employee() - { - Id = 20, - Name = "Mike" - }; - } - - [HttpPost] - public IActionResult CreateEmployee([FromBody]Employee employee) - { - if(!ModelState.IsValid) - { - return BadRequest(ModelState); - } - - return new ObjectResult(employee); - } - - public class Employee - { - public int Id { get; set; } - - public string Name { get; set; } + return "Hello World!"; } } } diff --git a/test/WebSites/FormatterWebSite/Program.cs b/test/WebSites/FormatterWebSite/Program.cs new file mode 100644 index 0000000000..15970bd9eb --- /dev/null +++ b/test/WebSites/FormatterWebSite/Program.cs @@ -0,0 +1,23 @@ +// 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.IO; +using Microsoft.AspNetCore.Hosting; + +namespace FormatterWebSite +{ + public class Program + { + public static void Main(string[] args) + { + var host = new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration() + .Build(); + + host.Run(); + } + } +} diff --git a/test/WebSites/FormatterWebSite/Startup.cs b/test/WebSites/FormatterWebSite/Startup.cs index 810bc98020..a2d53bf982 100644 --- a/test/WebSites/FormatterWebSite/Startup.cs +++ b/test/WebSites/FormatterWebSite/Startup.cs @@ -1,9 +1,7 @@ // 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.IO; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.DependencyInjection; @@ -34,18 +32,6 @@ public void Configure(IApplicationBuilder app) defaults: new { controller = "Home", action = "Index" }); }); } - - public static void Main(string[] args) - { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() - .Build(); - - host.Run(); - } } } diff --git a/test/WebSites/FormatterWebSite/StartupWithRespectBrowserAcceptHeader.cs b/test/WebSites/FormatterWebSite/StartupWithRespectBrowserAcceptHeader.cs new file mode 100644 index 0000000000..217bc78573 --- /dev/null +++ b/test/WebSites/FormatterWebSite/StartupWithRespectBrowserAcceptHeader.cs @@ -0,0 +1,31 @@ +// 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 Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace FormatterWebSite +{ + public class StartupWithRespectBrowserAcceptHeader + { + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(options => + { + options.RespectBrowserAcceptHeader = true; + }); + } + + public void Configure(IApplicationBuilder app) + { + app.UseCultureReplacer(); + + app.UseMvc(routes => + { + routes.MapRoute("ActionAsMethod", "{controller}/{action}", + defaults: new { controller = "Home", action = "Index" }); + }); + } + } +} +