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:
- conventionally name the view the same as the return type
- return a
View
object - attribute your return type with a
ViewAttribute
- define the view mapping in a
ViewConfig
You also need to register the formatter 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.