This commit is contained in:
N. Taylor Mullen 2014-12-15 15:25:12 -08:00 committed by Mugdha Kulkarni
parent e5176d22f6
commit cf7d428abd
4 changed files with 72 additions and 0 deletions

View File

@ -15,6 +15,7 @@ using MvcSample.Web.Services;
#if ASPNET50
using Autofac;
using Microsoft.Framework.DependencyInjection.Autofac;
using Microsoft.AspNet.Mvc.Core.Filters;
#endif
namespace MvcSample.Web
@ -114,6 +115,8 @@ namespace MvcSample.Web
{
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
routes.MapRoute("formatRoute", "{controller}/{action}/{format}");
routes.MapRoute(
"controllerActionRoute",
"{controller}/{action}",

View File

@ -0,0 +1,49 @@
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
using Microsoft.Framework.OptionsModel;
using System;
using System.Collections.Generic;
namespace Microsoft.AspNet.Mvc.Core.Filters
{
public class UrlExtensionFilter : IResultFilter
{
//private Dictionary<string, MediaTypeHeaderValue> FormatContentTypeMap =
// new Dictionary<string, MediaTypeHeaderValue>();
//public void AddFormatMapping(string format, MediaTypeHeaderValue contentType)
//{
// if(FormatContentTypeMap.ContainsKey(format))
// {
// FormatContentTypeMap.Remove(format);
// }
// FormatContentTypeMap.Add(format, contentType);
//}
public void OnResultExecuting([NotNull] ResultExecutingContext context)
{
var options = (IOptions<MvcOptions>)context.HttpContext.RequestServices.GetService(typeof(IOptions<MvcOptions>));
if (context.RouteData.Values.ContainsKey("format"))
{
var format = context.RouteData.Values["format"].ToString();
var contentType = options.Options.OutputFormatterOptions.GetContentTypeForFormat(format);
if (contentType != null)
{
var objectResult = context.Result as ObjectResult;
objectResult.ContentTypes.Clear();
objectResult.ContentTypes.Add(contentType);
}
else
{
throw new InvalidOperationException("No formatter exists for format:" + format);
}
}
}
public void OnResultExecuted([NotNull] ResultExecutedContext context)
{
}
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.AspNet.Mvc.ApplicationModels;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
namespace Microsoft.AspNet.Mvc
{
@ -17,6 +18,7 @@ namespace Microsoft.AspNet.Mvc
{
private AntiForgeryOptions _antiForgeryOptions = new AntiForgeryOptions();
private int _maxModelStateErrors = ModelStateDictionary.DefaultMaxAllowedErrors;
//private OutputFormatterOptions _outputFormatterOptions = new OutputFormatterOptions();
public MvcOptions()
{
@ -27,6 +29,7 @@ namespace Microsoft.AspNet.Mvc
OutputFormatters = new List<OutputFormatterDescriptor>();
InputFormatters = new List<InputFormatterDescriptor>();
Filters = new List<IFilter>();
OutputFormatterOptions = new OutputFormatterOptions();
}
/// <summary>
@ -52,6 +55,8 @@ namespace Microsoft.AspNet.Mvc
}
}
public OutputFormatterOptions OutputFormatterOptions { get; }
/// <summary>
/// Gets a list of <see cref="IFilter"/> which are used to construct filters that
/// apply to all actions.
@ -64,6 +69,16 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
public List<OutputFormatterDescriptor> OutputFormatters { get; private set; }
/// <summary>
/// Sets the mapping for output format specified in URL (extension) and content type
/// </summary>
/// <param name="format">URL extension for output format</param>
/// <param name="contentType">Content type mapping to the format</param>
public void AddFormatMapping(string format, MediaTypeHeaderValue contentType)
{
OutputFormatterOptions.AddFormatMapping(format, contentType);
}
/// <summary>
/// Gets a list of the <see cref="InputFormatterDescriptor" /> which are used to construct
/// a list of <see cref="IInputFormatter"/> by <see cref="IInputFormattersProvider"/>.

View File

@ -7,6 +7,7 @@ using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Razor;
using Microsoft.Framework.OptionsModel;
using Newtonsoft.Json.Linq;
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
namespace Microsoft.AspNet.Mvc
{
@ -45,6 +46,10 @@ namespace Microsoft.AspNet.Mvc
options.OutputFormatters.Add(new StringOutputFormatter());
options.OutputFormatters.Add(new JsonOutputFormatter());
// Set up default mapping for xml and json extensions to content type
options.AddFormatMapping("json", MediaTypeHeaderValue.Parse("application/json"));
options.AddFormatMapping("xml", MediaTypeHeaderValue.Parse("application/xml"));
// Set up default input formatters.
options.InputFormatters.Add(new JsonInputFormatter());