Changing produces and format filter interaction
This commit is contained in:
parent
7ee80020e1
commit
addd8dd5d2
|
|
@ -5,7 +5,6 @@ using Microsoft.AspNet.Mvc;
|
|||
|
||||
namespace MvcSample.Web.Controllers
|
||||
{
|
||||
[FormatFilter]
|
||||
public class FormatFilterController : Controller
|
||||
{
|
||||
public Product GetProduct(int id)
|
||||
|
|
|
|||
|
|
@ -63,9 +63,8 @@ namespace MvcSample.Web
|
|||
services.Configure<MvcOptions>(options =>
|
||||
{
|
||||
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
|
||||
options.AddXmlDataContractSerializerFormatter();
|
||||
var formatFilter = new FormatFilterAttribute();
|
||||
options.Filters.Add(formatFilter);
|
||||
options.AddXmlDataContractSerializerFormatter();
|
||||
options.Filters.Add(new FormatFilterAttribute());
|
||||
});
|
||||
services.Configure<RazorViewEngineOptions>(options =>
|
||||
{
|
||||
|
|
@ -108,9 +107,7 @@ namespace MvcSample.Web
|
|||
{
|
||||
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
|
||||
options.AddXmlDataContractSerializerFormatter();
|
||||
|
||||
var formatFilter = new FormatFilterAttribute();
|
||||
options.Filters.Add(formatFilter);
|
||||
options.Filters.Add(new FormatFilterAttribute());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,19 +13,18 @@ using System.Collections.Generic;
|
|||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// This will look at the format parameter if present in the route data or query data and sets the content type in
|
||||
/// ObjectResult corresponding to the format value.
|
||||
/// A filter which will use the format value in the route data or query string to set the content type on an
|
||||
/// <see cref="ObjectResult" /> returned from an action.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class FormatFilterAttribute : Attribute, IFormatFilter, IResourceFilter, IResultFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// As a resourceFilter, this filter looks at the request and rejects it
|
||||
/// before going ahead if
|
||||
/// As a <see cref="IResourceFilter"/>, this filter looks at the request and rejects it before going ahead if
|
||||
/// 1. The format in the request doesnt match any format in the map.
|
||||
/// 2. If there is a conflicting producesFilter.
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="context">The <see cref="ResourceExecutingContext"/>.</param>
|
||||
public void OnResourceExecuting([NotNull] ResourceExecutingContext context)
|
||||
{
|
||||
var format = GetFormat(context);
|
||||
|
|
@ -40,21 +39,23 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
else
|
||||
{
|
||||
var responseTypeFilters = context.Filters.OfType<IApiResponseMetadataProvider>();
|
||||
if (responseTypeFilters.Count() != 0)
|
||||
var responseTypeFilters = context.Filters.OfType<IApiResponseMetadataProvider>();
|
||||
var contentTypes = new List<MediaTypeHeaderValue>();
|
||||
|
||||
foreach (var filter in responseTypeFilters)
|
||||
{
|
||||
var contentTypes = new List<MediaTypeHeaderValue>();
|
||||
filter.SetContentTypes(contentTypes);
|
||||
}
|
||||
|
||||
foreach (var filter in responseTypeFilters)
|
||||
{
|
||||
filter.SetContentTypes(contentTypes);
|
||||
}
|
||||
|
||||
if (!contentTypes.Any(c => c.IsSubsetOf(formatContentType)))
|
||||
if (contentTypes.Count() != 0)
|
||||
{
|
||||
// If formatfilterContentType is not subset of any of the content types produced by
|
||||
// IApiResponseMetadataProviders, return 404
|
||||
if (!contentTypes.Any(c => formatContentType.IsSubsetOf(c)))
|
||||
{
|
||||
context.Result = new HttpNotFoundResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -66,19 +67,19 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method looks at the format that request has and sets it in the context
|
||||
/// Sets a Content Type on an <see cref="ObjectResult" /> using a format value from the request.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="ResultExecutingContext"/>.</param>
|
||||
public void OnResultExecuting([NotNull] ResultExecutingContext context)
|
||||
{
|
||||
var format = GetFormat(context);
|
||||
if (format != null)
|
||||
if (!string.IsNullOrEmpty(format))
|
||||
{
|
||||
var contentType = GetContentType(format, context);
|
||||
Debug.Assert(contentType != null);
|
||||
|
||||
var objectResult = context.Result as ObjectResult;
|
||||
if (objectResult != null)
|
||||
{
|
||||
var contentType = GetContentType(format, context);
|
||||
Debug.Assert(contentType != null);
|
||||
objectResult.ContentTypes.Clear();
|
||||
objectResult.ContentTypes.Add(contentType);
|
||||
}
|
||||
|
|
@ -92,20 +93,15 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks at the current request for the format parameter. If it contains format, it returns the content type
|
||||
/// for it.
|
||||
/// If the current request contains format value, returns true. It means the format filter is going to execute.
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public MediaTypeHeaderValue GetContentTypeForCurrentRequest(FilterContext context)
|
||||
/// <param name="context">The <see cref="FilterContext"/></param>
|
||||
/// <returns>If the filter is active and will execute.</returns>
|
||||
public bool IsActive(FilterContext context)
|
||||
{
|
||||
var format = GetFormat(context);
|
||||
if (format != null && !string.IsNullOrEmpty(format.ToString()))
|
||||
{
|
||||
return GetContentType(format, context);
|
||||
}
|
||||
|
||||
return null;
|
||||
return !string.IsNullOrEmpty(format);
|
||||
}
|
||||
|
||||
private string GetFormat(FilterContext context)
|
||||
|
|
@ -114,25 +110,17 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
if (!context.RouteData.Values.TryGetValue("format", out format))
|
||||
{
|
||||
if (context.HttpContext.Request.Query.ContainsKey("format"))
|
||||
{
|
||||
format = context.HttpContext.Request.Query.Get("format");
|
||||
return format.ToString();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return format.ToString();
|
||||
format = context.HttpContext.Request.Query["format"];
|
||||
}
|
||||
|
||||
return null;
|
||||
return (string)format;
|
||||
}
|
||||
|
||||
private MediaTypeHeaderValue GetContentType(object format, FilterContext context)
|
||||
private MediaTypeHeaderValue GetContentType(string format, FilterContext context)
|
||||
{
|
||||
Debug.Assert(format != null);
|
||||
var options = context.HttpContext.RequestServices.GetRequiredService<IOptions<MvcOptions>>();
|
||||
var contentType = options.Options.FormatterMappings.GetMediaTypeForFormat(format.ToString());
|
||||
var contentType = options.Options.FormatterMappings.GetMediaTypeMappingForFormat(format);
|
||||
|
||||
return contentType;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,17 @@ using System;
|
|||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement this interface if you want to have your own implementation of FormatFilter. A FormatFilter decides
|
||||
/// what content type to use if the format is present in the Url.
|
||||
/// A filter which produces a desired content type for the current request.
|
||||
/// </summary>
|
||||
/// <remarks>A FormatFilter decides what content type to use if a format value is present in the URL.
|
||||
/// </remarks>
|
||||
public interface IFormatFilter : IFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the <see cref="MediaTypeHeaderValue"/> registered fot the format in the request.
|
||||
/// Returns true if the filter is going to be executed for the current request.
|
||||
/// </summary>
|
||||
MediaTypeHeaderValue GetContentTypeForCurrentRequest(FilterContext context);
|
||||
/// <param name="context">The <see cref="FilterContext"/></param>
|
||||
/// <returns>If the filter will execute</returns>
|
||||
bool IsActive(FilterContext context);
|
||||
}
|
||||
}
|
||||
|
|
@ -34,10 +34,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
if (objectResult != null)
|
||||
{
|
||||
// Check if FormatFilter has already set the content type
|
||||
// If it has, dont override it
|
||||
var formatFilter = context.Filters.OfType<IFormatFilter>().LastOrDefault();
|
||||
if (formatFilter == null || formatFilter.GetContentTypeForCurrentRequest(context) == null)
|
||||
// Check if there are any IFormatFilter in the pipeline, and if any of them is active. If there is one,
|
||||
// do not override the content type value.
|
||||
if (context.Filters.OfType<IFormatFilter>().All(f => !f.IsActive(context)))
|
||||
{
|
||||
SetContentTypes(objectResult.ContentTypes);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ using Microsoft.AspNet.Mvc.Core;
|
|||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to specify mapping between the Url Format and corresponding <see cref="MediaTypeHeaderValue"/>.
|
||||
/// Used to specify mapping between the URL Format and corresponding <see cref="MediaTypeHeaderValue"/>.
|
||||
/// </summary>
|
||||
public class FormatterMappings
|
||||
{
|
||||
|
|
@ -19,21 +19,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
new Dictionary<string, MediaTypeHeaderValue>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// This will set mapping for the format to specified <see cref="MediaTypeHeaderValue"/>.
|
||||
/// Sets mapping for the format to specified <see cref="MediaTypeHeaderValue"/>.
|
||||
/// If the format already exists, the <see cref="MediaTypeHeaderValue"/> will be overwritten with the new value.
|
||||
/// </summary>
|
||||
/// <param name="format">format value</param>
|
||||
/// <param name="contentType">The <see cref="MediaTypeHeaderValue"/> for the format value</param>
|
||||
public void SetMediaTypeMappingForFormat([NotNull] string format, [NotNull] MediaTypeHeaderValue contentType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(format))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "format");
|
||||
}
|
||||
|
||||
if (contentType == null)
|
||||
{
|
||||
throw new ArgumentException((Resources.ArgumentCannotBeNullOrEmpty), "contentType");
|
||||
}
|
||||
|
||||
ValidateContentType(contentType);
|
||||
format = RemovePeriodIfPresent(format);
|
||||
_map[format] = contentType;
|
||||
}
|
||||
|
|
@ -41,14 +34,26 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <summary>
|
||||
/// Gets <see cref="MediaTypeHeaderValue"/> for the specified format.
|
||||
/// </summary>
|
||||
public MediaTypeHeaderValue GetMediaTypeForFormat(string format)
|
||||
/// <param name="format">format value.</param>
|
||||
/// <returns>The <see cref="MediaTypeHeaderValue"/> for input format</returns>
|
||||
public MediaTypeHeaderValue GetMediaTypeMappingForFormat([NotNull] string format)
|
||||
{
|
||||
format = RemovePeriodIfPresent(format);
|
||||
|
||||
MediaTypeHeaderValue value = null;
|
||||
_map.TryGetValue(format, out value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private void ValidateContentType(MediaTypeHeaderValue contentType)
|
||||
{
|
||||
if(contentType.Type == "*" || contentType.SubType == "*")
|
||||
{
|
||||
throw new ArgumentException(string.Format(Resources.FormatterMappings_NotValidMediaType, contentType));
|
||||
}
|
||||
}
|
||||
|
||||
private string RemovePeriodIfPresent(string format)
|
||||
{
|
||||
if (format.StartsWith("."))
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to specify mapping between the Url Format and corresponding <see cref="MediaTypeHeaderValue"/>.
|
||||
/// Used to specify mapping between the URL Format and corresponding <see cref="MediaTypeHeaderValue"/>.
|
||||
/// </summary>
|
||||
public FormatterMappings FormatterMappings { get; }
|
||||
|
||||
|
|
|
|||
|
|
@ -562,6 +562,11 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
get { return GetString("Common_ValueNotValidForProperty"); }
|
||||
}
|
||||
|
||||
internal static string FormatterMappings_NotValidMediaType
|
||||
{
|
||||
get { return GetString("FormatterMappings_NotValidMediaType"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value '{0}' is invalid.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -450,6 +450,9 @@
|
|||
</data>
|
||||
<data name="ApiExplorer_UnsupportedAction" xml:space="preserve">
|
||||
<value>The action '{0}' has ApiExplorer enabled, but is using conventional routing. Only actions which use attribute routing support ApiExplorer.</value>
|
||||
</data>
|
||||
<data name="FormatterMappings_NotValidMediaType" xml:space="preserve">
|
||||
<value>The media type {0} is not valid. The media type containing "<mediatype>/*" are not supported.</value>
|
||||
</data>
|
||||
<data name="RemoteAttribute_NoUrlFound" xml:space="preserve">
|
||||
<value>No URL for remote validation could be found.</value>
|
||||
|
|
|
|||
|
|
@ -171,8 +171,8 @@ namespace Microsoft.AspNet.Mvc
|
|||
[InlineData("json", FormatSource.RouteData, "application/json")]
|
||||
[InlineData("json", FormatSource.QueryData, "application/json")]
|
||||
public void FormatFilter_ContextContainsFormat_ContainsProducesFilter_Matching(
|
||||
string format,
|
||||
FormatSource place,
|
||||
string format,
|
||||
FormatSource place,
|
||||
string contentType)
|
||||
{
|
||||
// Arrange
|
||||
|
|
@ -188,23 +188,38 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void FormatFilter_ContextContainsFormat_ContainsProducesFilter_WildCardMatching()
|
||||
public void FormatFilter_LessSpecificThan_Produces()
|
||||
{
|
||||
// Arrange
|
||||
var produces = new ProducesAttribute(
|
||||
"application/baz",
|
||||
new string[] { "application/foo", "text/bar" });
|
||||
var context = CreateResourceExecutingContext(new IFilter[] { produces }, "star", FormatSource.RouteData);
|
||||
var produces = new ProducesAttribute("application/xml;version=1", new string [] { });
|
||||
var context = CreateResourceExecutingContext(new IFilter[] { produces }, "xml", FormatSource.RouteData);
|
||||
var options = context.HttpContext.RequestServices.GetService<IOptions<MvcOptions>>();
|
||||
options.Options.FormatterMappings.SetMediaTypeMappingForFormat("star", MediaTypeHeaderValue.Parse("application/*"));
|
||||
options.Options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml"));
|
||||
var filter = new FormatFilterAttribute();
|
||||
|
||||
// Act
|
||||
filter.OnResourceExecuting(context);
|
||||
|
||||
// Assert
|
||||
Assert.Null(context.Result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FormatFilter_MoreSpecificThan_Produces()
|
||||
{
|
||||
// Arrange
|
||||
var produces = new ProducesAttribute("application/xml", new string[] { });
|
||||
var context = CreateResourceExecutingContext(new IFilter[] { produces }, "xml", FormatSource.RouteData);
|
||||
var options = context.HttpContext.RequestServices.GetService<IOptions<MvcOptions>>();
|
||||
options.Options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml;version=1"));
|
||||
var filter = new FormatFilterAttribute();
|
||||
|
||||
// Act
|
||||
filter.OnResourceExecuting(context);
|
||||
|
||||
// Assert
|
||||
Assert.Null(context.Result);
|
||||
var actionResult = context.Result;
|
||||
Assert.IsType<HttpNotFoundResult>(actionResult);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -241,7 +256,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
var resourceExecutingContext = CreateResourceExecutingContext(
|
||||
new IFilter[] { },
|
||||
format,
|
||||
FormatSource.RouteData);
|
||||
place);
|
||||
var filter = new FormatFilterAttribute();
|
||||
|
||||
// Act
|
||||
|
|
@ -252,31 +267,24 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("json", FormatSource.RouteData, "application/json")]
|
||||
[InlineData("json", FormatSource.QueryData, "application/json")]
|
||||
[InlineData("", FormatSource.RouteAndQueryData, null)]
|
||||
[InlineData(null, FormatSource.RouteAndQueryData, null)]
|
||||
public void FormatFilter_GetContentTypeForRequest(
|
||||
[InlineData("json", FormatSource.RouteData, true)]
|
||||
[InlineData("json", FormatSource.QueryData, true )]
|
||||
[InlineData("", FormatSource.RouteAndQueryData, false)]
|
||||
[InlineData(null, FormatSource.RouteAndQueryData, false)]
|
||||
public void FormatFilter_IsActive(
|
||||
string format,
|
||||
FormatSource place,
|
||||
string contentType)
|
||||
bool expected)
|
||||
{
|
||||
// Arrange
|
||||
var resourceExecutingContext = CreateResourceExecutingContext(
|
||||
new IFilter[] { },
|
||||
format,
|
||||
FormatSource.RouteData);
|
||||
var resultExecutingContext = CreateResultExecutingContext(format, place);
|
||||
var filter = new FormatFilterAttribute();
|
||||
var returnContentType = filter.GetContentTypeForCurrentRequest(resourceExecutingContext);
|
||||
|
||||
// Act
|
||||
var isActive = filter.IsActive(resultExecutingContext);
|
||||
|
||||
MediaTypeHeaderValue mediaType = null;
|
||||
if (returnContentType != null)
|
||||
{
|
||||
mediaType = MediaTypeHeaderValue.Parse("application/json");
|
||||
}
|
||||
|
||||
Assert.Equal(mediaType, returnContentType);
|
||||
// Assert
|
||||
Assert.Equal(expected, isActive);
|
||||
}
|
||||
|
||||
private static ResourceExecutingContext CreateResourceExecutingContext(
|
||||
|
|
@ -332,7 +340,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
if (place == FormatSource.QueryData || place == FormatSource.RouteAndQueryData)
|
||||
{
|
||||
httpContext.Setup(c => c.Request.Query.ContainsKey("format")).Returns(true);
|
||||
httpContext.Setup(c => c.Request.Query.Get("format")).Returns(format);
|
||||
httpContext.Setup(c => c.Request.Query["format"]).Returns(format);
|
||||
}
|
||||
else if (place == null && format == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -39,16 +39,38 @@ namespace Microsoft.AspNet.Mvc.Test
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProducesContentAttribute_FormatFilterAttribute()
|
||||
public async Task ProducesContentAttribute_FormatFilterAttribute_NotActive()
|
||||
{
|
||||
// Arrange
|
||||
var mediaType1 = MediaTypeHeaderValue.Parse("application/xml");
|
||||
var mediaType2 = MediaTypeHeaderValue.Parse("application/json");
|
||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||
|
||||
var formatFilter = new Mock<IFormatFilter>();
|
||||
formatFilter.Setup(f => f.GetContentTypeForCurrentRequest(It.IsAny<FilterContext>()))
|
||||
.Returns(mediaType2);
|
||||
formatFilter.Setup(f => f.IsActive(It.IsAny<FilterContext>()))
|
||||
.Returns(false);
|
||||
|
||||
var filters = new IFilter[] { producesContentAttribute, formatFilter.Object };
|
||||
var resultExecutingContext = CreateResultExecutingContext(filters);
|
||||
|
||||
var next = new ResultExecutionDelegate(
|
||||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||
|
||||
// Act
|
||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
||||
|
||||
// Assert
|
||||
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
||||
Assert.Equal(1, objectResult.ContentTypes.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProducesContentAttribute_FormatFilterAttribute_Active()
|
||||
{
|
||||
// Arrange
|
||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||
|
||||
var formatFilter = new Mock<IFormatFilter>();
|
||||
formatFilter.Setup(f => f.IsActive(It.IsAny<FilterContext>()))
|
||||
.Returns(true);
|
||||
|
||||
var filters = new IFilter[] { producesContentAttribute, formatFilter.Object };
|
||||
var resultExecutingContext = CreateResultExecutingContext(filters);
|
||||
|
|
@ -64,7 +86,6 @@ namespace Microsoft.AspNet.Mvc.Test
|
|||
Assert.Equal(0, objectResult.ContentTypes.Count);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "")]
|
||||
[InlineData("application/xml,, application/json", "")]
|
||||
|
|
|
|||
|
|
@ -24,31 +24,25 @@ namespace Microsoft.AspNet.Mvc
|
|||
options.SetMediaTypeMappingForFormat(setFormat, mediaType);
|
||||
|
||||
// Act
|
||||
var returnMediaType = options.GetMediaTypeForFormat(getFormat);
|
||||
var returnMediaType = options.GetMediaTypeMappingForFormat(getFormat);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(mediaType, returnMediaType);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData("xml", null)]
|
||||
[InlineData(".json", null)]
|
||||
[InlineData(null, "application/json")]
|
||||
[InlineData("", "text/foo")]
|
||||
public void FormatterMappings_SetFormatMapping_Invalid(string format, string contentType)
|
||||
[InlineData("application/*")]
|
||||
[InlineData("*/json")]
|
||||
[InlineData("*/*")]
|
||||
public void FormatterMappings_Wildcardformat(string format)
|
||||
{
|
||||
// Arrange
|
||||
MediaTypeHeaderValue mediaType = null;
|
||||
if (!string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
mediaType = MediaTypeHeaderValue.Parse(contentType);
|
||||
}
|
||||
var options = new FormatterMappings();
|
||||
var expected = string.Format(@"The media type {0} is not valid. The media type containing ""<mediatype>/*"" are not supported.", format);
|
||||
|
||||
var options = new FormatterMappings();
|
||||
var expectedError = "Value cannot be null or empty." + Environment.NewLine + "Parameter name: format";
|
||||
|
||||
// Act and Assert
|
||||
Assert.Throws<ArgumentException>(() => options.SetMediaTypeMappingForFormat(format, mediaType));
|
||||
// Act and assert
|
||||
var exception = Assert.Throws<ArgumentException>(() => options.SetMediaTypeMappingForFormat("star", MediaTypeHeaderValue.Parse(format)));
|
||||
Assert.Equal(expected, exception.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -141,10 +141,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
var client = server.CreateClient();
|
||||
|
||||
// Act
|
||||
var response = await client.GetAsync("http://localhost/ProducesDerived/ReturnClassName.json");
|
||||
var response = await client.GetAsync("http://localhost/ProducesOverride/ReturnClassName");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(@"ProducesOverrideController", await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,4 +20,9 @@
|
|||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
"ErrorPageMiddlewareWebSite": "1.0.0",
|
||||
"FilesWebSite": "1.0.0",
|
||||
"FiltersWebSite": "1.0.0",
|
||||
"FormatFilterWebSite": "1.0.0-*",
|
||||
"FormatterWebSite": "1.0.0",
|
||||
"InlineConstraintsWebSite": "1.0.0",
|
||||
"LoggingWebSite": "1.0.0",
|
||||
|
|
@ -44,8 +45,7 @@
|
|||
"MvcTagHelpersWebSite": "1.0.0",
|
||||
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
|
||||
"xunit.runner.kre": "1.0.0-*",
|
||||
"Microsoft.AspNet.WebUtilities": "1.0.0-*",
|
||||
"FormatFilterWebSite": "1.0.0-*"
|
||||
"Microsoft.AspNet.WebUtilities": "1.0.0-*"
|
||||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.kre"
|
||||
|
|
|
|||
|
|
@ -37,6 +37,6 @@ namespace ConnegWebSite
|
|||
public virtual string ReturnClassNameContentTypeOnDerivedAction()
|
||||
{
|
||||
return "ProducesContentBaseController";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,6 @@ namespace ConnegWebSite
|
|||
{
|
||||
// should be written using the content defined at derived class's class.
|
||||
return "ProducesContentOnClassController";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ namespace ConnegWebSite
|
|||
app.UseMvc(routes =>
|
||||
{
|
||||
routes.MapRoute("ActionAsMethod", "{controller}/{action}",
|
||||
defaults: new { controller = "Home", action = "Index" });
|
||||
defaults: new { controller = "Home", action = "Index" });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,4 @@
|
|||
<DevelopmentServerPort>49641</DevelopmentServerPort>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
||||
|
|
@ -5,7 +5,6 @@ using System.Security.Claims;
|
|||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Security;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
|
||||
namespace FiltersWebSite
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
// 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 FormatFilterWebSite
|
||||
{
|
||||
public class ProducesBaseController : Controller
|
||||
{
|
||||
[Produces("application/custom_ProducesBaseController_Action")]
|
||||
public virtual string ReturnClassName()
|
||||
{
|
||||
// Should be written using the action's content type. Overriding the one at the class.
|
||||
return "ProducesBaseController";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,11 +5,11 @@ using Microsoft.AspNet.Mvc;
|
|||
|
||||
namespace FormatFilterWebSite
|
||||
{
|
||||
[Produces("application/custom_ProducesOverrideController")]
|
||||
public class ProducesOverrideController : ProducesBaseController
|
||||
[Produces("application/custom_ProducesController")]
|
||||
public class ProducesOverrideController
|
||||
{
|
||||
[FormatFilter]
|
||||
public override string ReturnClassName()
|
||||
[Produces("application/ProducesMethod")]
|
||||
public string ReturnClassName()
|
||||
{
|
||||
// should be written using the content defined at base class's action.
|
||||
return "ProducesOverrideController";
|
||||
|
|
|
|||
Loading…
Reference in New Issue