Skip to content

Commit 78ed919

Browse files
committed
Add RazorViewFormatter to ease creation of a Razor-based HtmlMediaTypeViewFormatter instance.
1 parent cdaf218 commit 78ed919

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using WebApiContrib.Formatting.Html;
2+
using WebApiContrib.Formatting.Html.Formatting;
3+
4+
namespace WebApiContrib.Formatting.Razor
5+
{
6+
/// <summary>
7+
/// <see cref="HtmlMediaTypeFormatter"/> using the Razor syntax.
8+
/// </summary>
9+
public sealed class RazorViewFormatter : HtmlMediaTypeViewFormatter
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="RazorViewFormatter"/>.
13+
/// </summary>
14+
/// <param name="siteRootPath">The root path containing view files. This defaults to "~/Views".</param>
15+
/// <param name="viewLocator">The <see cref="IViewLocator"/> instance used to locate the correct view. This defaults to <see cref="RazorViewLocator"/>.</param>
16+
/// <param name="viewParser">The <see cref="IViewParser"/> instance used to parse the view. This defaults to <see cref="RazorViewParser"/>.</param>
17+
public RazorViewFormatter(string siteRootPath = null, IViewLocator viewLocator = null, IViewParser viewParser = null)
18+
: base(siteRootPath, viewLocator ?? new RazorViewLocator(), viewParser ?? new RazorViewParser())
19+
{
20+
}
21+
}
22+
}

src/WebApiContrib.Formatting.Razor/WebApiContrib.Formatting.Razor.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
</ItemGroup>
6464
<ItemGroup>
6565
<Compile Include="EmbeddedResolver.cs" />
66+
<Compile Include="RazorViewFormatter.cs" />
6667
<Compile Include="TemplateResolver.cs" />
6768
<Compile Include="RazorViewLocator.cs" />
6869
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -80,4 +81,4 @@
8081
<Target Name="AfterBuild">
8182
</Target>
8283
-->
83-
</Project>
84+
</Project>

test/WebApiContrib.Formatting.Razor.Tests/ViewEngineTests.cs

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Net.Http;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using System.Web.Http;
45
using NUnit.Framework;
56
using WebApiContrib.Formatting.Html;
67
using WebApiContrib.Formatting.Html.Formatting;
@@ -10,21 +11,32 @@ namespace WebApiContrib.Formatting.Razor.Tests
1011
[TestFixture]
1112
public class ViewEngineTests
1213
{
13-
private HtmlMediaTypeViewFormatter _formatter;
14+
private HttpRequestMessage _request;
1415

1516
[SetUp]
1617
public void Before()
1718
{
18-
_formatter = new HtmlMediaTypeViewFormatter();
19-
GlobalViews.DefaultViewParser = new RazorViewParser();
20-
GlobalViews.DefaultViewLocator = new RazorViewLocator();
19+
var formatter = new RazorViewFormatter();
20+
var config = new HttpConfiguration();
21+
config.Formatters.Add(formatter);
22+
23+
_request = new HttpRequestMessage();
24+
_request.SetConfiguration(config);
25+
_request.RegisterForDispose(config);
26+
}
27+
28+
[TearDown]
29+
public void After()
30+
{
31+
_request.Dispose();
32+
_request = null;
2133
}
2234

2335
[Test]
2436
public async Task render_simple_template()
2537
{
2638
var cts = new CancellationTokenSource();
27-
var view = new ViewResult(new HttpRequestMessage(), "Test1", new {Name = "foo"});
39+
var view = new ViewResult(_request, "Test1", new {Name = "foo"});
2840

2941
var response = await view.ExecuteAsync(cts.Token);
3042
var output = await response.Content.ReadAsStringAsync();
@@ -36,7 +48,7 @@ public async Task render_simple_template()
3648
public async Task render_template_with_embedded_layout()
3749
{
3850
var cts = new CancellationTokenSource();
39-
var view = new ViewResult(new HttpRequestMessage(), "Test2", new { Name = "foo" });
51+
var view = new ViewResult(_request, "Test2", new { Name = "foo" });
4052

4153
var response = await view.ExecuteAsync(cts.Token);
4254
var output = await response.Content.ReadAsStringAsync();
@@ -50,7 +62,14 @@ public async Task render_template_with_specified_resolver()
5062
var cts = new CancellationTokenSource();
5163
var resolver = new EmbeddedResolver(this.GetType());
5264
var formatter = new HtmlMediaTypeViewFormatter(null, new RazorViewLocator(), new RazorViewParser(resolver));
53-
var view = new ViewResult(new HttpRequestMessage(), "Test2", new { Name = "foo" });
65+
66+
// Replace the HTML formatter.
67+
var config = _request.GetConfiguration();
68+
var oldFormatter = config.Formatters.GetHtmlFormatter();
69+
config.Formatters.Remove(oldFormatter);
70+
config.Formatters.Add(formatter);
71+
72+
var view = new ViewResult(_request, "Test2", new { Name = "foo" });
5473

5574
var response = await view.ExecuteAsync(cts.Token);
5675
var output = await response.Content.ReadAsStringAsync();

0 commit comments

Comments
 (0)