Skip to content

Commit a266e49

Browse files
committedDec 9, 2013
Update README.
1 parent 78ed919 commit a266e49

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed
 

‎readme.md

+28-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,38 @@ Get it from [NuGet](http://nuget.org/packages/WebApiContrib.Formatting.Razor):
1010
WebApiContrib.Formatting.Razor lets you return HTML markup using three different mechanisms:
1111

1212
1. conventionally [name the view](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/tree/master/samples/MvcWebApiSiteTest/Views) the same as the [return type](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/blob/master/samples/MvcWebApiSiteTest/Controllers/CustomerController.cs#L14)
13-
2. [return a `View` object](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/blob/master/samples/MvcWebApiSiteTest/Controllers/HomeController.cs#L10)
13+
2. [return a `ViewResult` object](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/blob/master/samples/MvcWebApiSiteTest/Controllers/HomeController.cs#L10)
1414
3. [attribute your return type with a `ViewAttribute`](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/blob/master/samples/MvcWebApiSiteTest/Controllers/CustomerController.cs#L21)
1515
4. define the view mapping in a [`ViewConfig`](https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/blob/master/samples/MvcWebApiSiteTest/App_Start/ViewConfig.cs)
1616

17-
You also need to register the formatter and set the `GlobalViews.DefaultViewParser` and `GlobalViews.DefaultViewLocator`:
17+
The simplest way to register Razor as a formatter is to use the `RazorViewFormatter`:
1818

19-
GlobalConfiguration.Configuration.Formatters.Add(new HtmlMediaTypeViewFormatter());
19+
``` csharp
20+
config.Formatters.Add(new RazorViewFormatter());
21+
```
2022

21-
GlobalViews.DefaultViewParser = new RazorViewParser();
22-
GlobalViews.DefaultViewLocator = new RazorViewLocator();
23-
// If using ViewConfig:
24-
//ViewConfig.Register(GlobalViews.Views);
23+
The `RazorViewFormatter takes three, optional parameters:
24+
25+
1. `siteRootPath` specifies the root folder containing the view files. This looks in `~/Views` by default.
26+
2. `viewLocator` specifies an instance of `IViewLocator`, which is set to `RazorViewLocator` by default.
27+
3. `viewParser` specifies an instance of `IViewParser`, which is set to `RazorViewParser` by default.
28+
29+
You can pass these values into either `RazorViewFormatter` or `HtmlMediaTypeViewFormatter`. You may want to do this to use embedded view resources, for example.
30+
31+
``` csharp
32+
config.Formatters.Add(new RazorViewFormatter(null, new RazorViewLocator(), new RazorViewParser()));
33+
//config.Formatters.Add(new HtmlMediaTypeViewFormatter(null, new RazorViewLocator(), new RazorViewParser()));
34+
```
35+
36+
You may also want to register the `HtmlMediaTypeFormatter` using the default constructor and set the `GlobalViews.DefaultViewParser` and `GlobalViews.DefaultViewLocator`:
37+
38+
``` csharp
39+
GlobalConfiguration.Configuration.Formatters.Add(new HtmlMediaTypeViewFormatter());
40+
41+
GlobalViews.DefaultViewParser = new RazorViewParser();
42+
GlobalViews.DefaultViewLocator = new RazorViewLocator();
43+
// If using ViewConfig:
44+
//ViewConfig.Register(GlobalViews.Views);
45+
```
2546

2647
The [`GlobalViews`](https://github.com/WebApiContrib/WebApiContrib.Formatting.Html/blob/master/src/WebApiContrib.Formatting.Html/Configuration/GlobalViews.cs) configuration comes from the [WebApiContrib.Formatting.Html](https://github.com/WebApiContrib/WebApiContrib.Formatting.Html) project.

0 commit comments

Comments
 (0)
Please sign in to comment.