-
Notifications
You must be signed in to change notification settings - Fork 233
RazorViewFormatter
Project page: WebApiContrib.Formatting.Razor
RazorViewFormatter is a MediaTypeFormatter implementation for generating HTML markup for ASP.NET Web API applications.
Get it from NuGet:
Install-Package WebApiContrib.Formatting.Razor
WebApiContrib.Formatting.Razor lets you return HTML markup using three different mechanisms:
- conventionally name the view the same as the return type
- return a
ViewResultobject - attribute your return type with a
ViewAttribute - define the view mapping in a
ViewConfig
The simplest way to register Razor as a formatter is to use the RazorViewFormatter:
config.Formatters.Add(new RazorViewFormatter());The `RazorViewFormatter takes three, optional parameters:
-
siteRootPathspecifies the root folder containing the view files. This looks in~/Viewsby default. -
viewLocatorspecifies an instance ofIViewLocator, which is set toRazorViewLocatorby default. -
viewParserspecifies an instance ofIViewParser, which is set toRazorViewParserby default.
You can pass these values into either RazorViewFormatter or HtmlMediaTypeViewFormatter. You may want to do this to use embedded view resources, for example.
config.Formatters.Add(new RazorViewFormatter(null, new RazorViewLocator(), new RazorViewParser()));
//config.Formatters.Add(new HtmlMediaTypeViewFormatter(null, new RazorViewLocator(), new RazorViewParser()));You may also want to register the HtmlMediaTypeFormatter using the default constructor and set the GlobalViews.DefaultViewParser and GlobalViews.DefaultViewLocator:
GlobalConfiguration.Configuration.Formatters.Add(new HtmlMediaTypeViewFormatter());
GlobalViews.DefaultViewParser = new RazorViewParser();
GlobalViews.DefaultViewLocator = new RazorViewLocator();
// If using ViewConfig:
//ViewConfig.Register(GlobalViews.Views);The GlobalViews configuration comes from the WebApiContrib.Formatting.Html project.