Skip to content

WebApiContrib/WebApiContrib.Formatting.Razor

Repository files navigation

WebApiContrib.Formatting.Razor

A MediaTypeFormatter 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:

  1. conventionally name the view the same as the return type
  2. return a ViewResult object
  3. attribute your return type with a ViewAttribute
  4. 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:

  1. siteRootPath specifies the root folder containing the view files. This looks in ~/Views by default.
  2. viewLocator specifies an instance of IViewLocator, which is set to RazorViewLocator by default.
  3. viewParser specifies an instance of IViewParser, which is set to RazorViewParser by 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.