Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 33a7ba4

Browse files
N. Taylor MullenMugdha Kulkarni
N. Taylor Mullen
authored and
Mugdha Kulkarni
committed
React to aspnet/Razor#207 changes.
1 parent 91d7e38 commit 33a7ba4

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

samples/MvcSample.Web/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#if ASPNET50
1616
using Autofac;
1717
using Microsoft.Framework.DependencyInjection.Autofac;
18+
using Microsoft.AspNet.Mvc.Core.Filters;
1819
#endif
1920

2021
namespace MvcSample.Web
@@ -90,6 +91,7 @@ public void Configure(IApplicationBuilder app)
9091
services.Configure<MvcOptions>(options =>
9192
{
9293
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
94+
options.Filters.Add(typeof(UrlExtensionFilter));
9395
});
9496
});
9597
}
@@ -98,6 +100,8 @@ public void Configure(IApplicationBuilder app)
98100
{
99101
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
100102

103+
routes.MapRoute("formatRoute", "{controller}/{action}/{format}");
104+
101105
routes.MapRoute(
102106
"controllerActionRoute",
103107
"{controller}/{action}",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
2+
using Microsoft.Framework.OptionsModel;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.AspNet.Mvc.Core.Filters
7+
{
8+
public class UrlExtensionFilter : IResultFilter
9+
{
10+
//private Dictionary<string, MediaTypeHeaderValue> FormatContentTypeMap =
11+
// new Dictionary<string, MediaTypeHeaderValue>();
12+
13+
//public void AddFormatMapping(string format, MediaTypeHeaderValue contentType)
14+
//{
15+
// if(FormatContentTypeMap.ContainsKey(format))
16+
// {
17+
// FormatContentTypeMap.Remove(format);
18+
// }
19+
20+
// FormatContentTypeMap.Add(format, contentType);
21+
//}
22+
23+
public void OnResultExecuting([NotNull] ResultExecutingContext context)
24+
{
25+
var options = (IOptions<MvcOptions>)context.HttpContext.RequestServices.GetService(typeof(IOptions<MvcOptions>));
26+
27+
if (context.RouteData.Values.ContainsKey("format"))
28+
{
29+
var format = context.RouteData.Values["format"].ToString();
30+
var contentType = options.Options.OutputFormatterOptions.GetContentTypeForFormat(format);
31+
if (contentType != null)
32+
{
33+
var objectResult = context.Result as ObjectResult;
34+
objectResult.ContentTypes.Clear();
35+
objectResult.ContentTypes.Add(contentType);
36+
}
37+
else
38+
{
39+
throw new InvalidOperationException("No formatter exists for format:" + format);
40+
}
41+
}
42+
}
43+
44+
public void OnResultExecuted([NotNull] ResultExecutedContext context)
45+
{
46+
47+
}
48+
}
49+
}

src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNet.Mvc.ApplicationModels;
77
using Microsoft.AspNet.Mvc.Core;
88
using Microsoft.AspNet.Mvc.OptionDescriptors;
9+
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
910

1011
namespace Microsoft.AspNet.Mvc
1112
{
@@ -16,6 +17,7 @@ public class MvcOptions
1617
{
1718
private AntiForgeryOptions _antiForgeryOptions = new AntiForgeryOptions();
1819
private int _maxModelStateErrors = 200;
20+
//private OutputFormatterOptions _outputFormatterOptions = new OutputFormatterOptions();
1921

2022
public MvcOptions()
2123
{
@@ -26,6 +28,7 @@ public MvcOptions()
2628
OutputFormatters = new List<OutputFormatterDescriptor>();
2729
InputFormatters = new List<InputFormatterDescriptor>();
2830
Filters = new List<IFilter>();
31+
OutputFormatterOptions = new OutputFormatterOptions();
2932
}
3033

3134
/// <summary>
@@ -51,6 +54,8 @@ public AntiForgeryOptions AntiForgeryOptions
5154
}
5255
}
5356

57+
public OutputFormatterOptions OutputFormatterOptions { get; }
58+
5459
/// <summary>
5560
/// Gets a list of <see cref="IFilter"/> which are used to construct filters that
5661
/// apply to all actions.
@@ -63,6 +68,16 @@ public AntiForgeryOptions AntiForgeryOptions
6368
/// </summary>
6469
public List<OutputFormatterDescriptor> OutputFormatters { get; private set; }
6570

71+
/// <summary>
72+
/// Sets the mapping for output format specified in URL (extension) and content type
73+
/// </summary>
74+
/// <param name="format">URL extension for output format</param>
75+
/// <param name="contentType">Content type mapping to the format</param>
76+
public void AddFormatMapping(string format, MediaTypeHeaderValue contentType)
77+
{
78+
OutputFormatterOptions.AddFormatMapping(format, contentType);
79+
}
80+
6681
/// <summary>
6782
/// Gets a list of the <see cref="InputFormatterDescriptor" /> which are used to construct
6883
/// a list of <see cref="IInputFormatter"/> by <see cref="IInputFormattersProvider"/>.

src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNet.Mvc.Razor;
88
using Microsoft.Framework.OptionsModel;
99
using Newtonsoft.Json.Linq;
10+
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
1011

1112
namespace Microsoft.AspNet.Mvc
1213
{
@@ -46,6 +47,10 @@ public static void ConfigureMvc(MvcOptions options)
4647
options.OutputFormatters.Add(
4748
new XmlDataContractSerializerOutputFormatter(XmlOutputFormatter.GetDefaultXmlWriterSettings()));
4849

50+
// Set up default mapping for xml and json extensions to content type
51+
options.AddFormatMapping("json", MediaTypeHeaderValue.Parse("application/json"));
52+
options.AddFormatMapping("xml", MediaTypeHeaderValue.Parse("application/xml"));
53+
4954
// Set up default input formatters.
5055
options.InputFormatters.Add(new JsonInputFormatter());
5156
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());

0 commit comments

Comments
 (0)