Calling the Embedded Views
Once the ViewEngine is registered you can start referring to the controllers and views by their viewname:
public class SampleController : Controller { public ActionResult ShowResourceHtml() { return View("eve-EV5.VE-Assets.Sample.SimpleHtml.index.html"); }Views consist of 2 parts:
- HTML markup that is usually stored as an embedded file, thus available by the embedded file system.
- An optional view class, which gives the availability to manipulate the HTML string further on the server side.
- "eve-": is the ViewEngine prefix, so essentially, we are explicitly asking this view to be processed by the ViewEngine we registered earlier.
- "EV5.VE-": is the reference to the embedded file system that we are using, this has been defied in the plugin interface for this EV5 module. See the previous article about creating plugins.
- "Assets.Sample.SimpleHtml.index.html": is the embedded path to the html file of our sample.
Click here to see the view in action.
Simple Html resource from view class
In the above case a dummy view class is generated without any implementation in the background, and the ViewEngine just renders the embedded html string. You can however implement the corresponding view class for this embedded resource.
Here is an example of a view class that serves as a code behind.
using EV5.Mvc.MEF; using EV5.Mvc.ViewEngine; using Microsoft.AspNetCore.Mvc.Rendering; namespace EV5.Samples.ViewEngine.Views { [EmbeddedView("View.and.Markup.Simple.Html.View")] [MarkupName("EV5.VE-Assets.Sample.SimpleHtml.index.html")] public class ViewAndMarkupSimpleResourceView : EmbeddedView { public override void ProcessView(ViewContext viewContext) { var node = this.HtmlDocument .Document .SelectSingleNode("//h1"); node.InnerHtml = "This is new generated content"; } } }
There are four key elements that the view implementation contains.
-
Class derived from EmbeddedView
Inheriting from EmbeddedView class ensure that the class has the ProcessView method that enables further processing of the HTML.
- EmbeddedView Attribute defines the viewname of the view. The ViewEngine will look first for a class with a specified viewname, this attributes marks the viewname for this class. If viewname is not defined for a view class the ViewEngine will never instantiate it.
- MarkupName Attribute define the embedded resource markup file The MarkupName attribute should contain the resource name for the html snippet corresponding to this view. This attribute is optional. If it is not defined the ViewEngine will use the viewname defined in the EmbeddedView attribute. If that is not found, and empty string will be used.
- Implementing the ProcessView method The ProcessView method is responsible to change the markup on the server side as needed. In this implementation the h1 content is changed in the code behind. You can use the various document helper functions to access and modify the markup content.
public ActionResult ShowViewAndMarkupSimpleResourceHtml() { return View("eve-View.and.Markup.Simple.Html.View"); }
Click here to see the view in action.
Simple Html from view class only
It is also possible not use any markup and rely only on the view class to define the HTML content.
Here is a basic hello world example:
using EV5.Mvc; using EV5.Mvc.MEF; using Microsoft.AspNetCore.Mvc.Rendering; namespace EV5.Samples.ViewEngine.Views { [EmbeddedView("eve-Just.Code.Simple.Html.View")] public class SimpleHtmlView : EmbeddedView { public override void ProcessView(ViewContext viewContext) { this.HtmlDocument.Document.LoadHtml(@"<html> <body> Hello World!!! </body> </html> "); } } }
The MarkupName attribute is omitted, thus the view will contain an empty document, but you can re-create it from code and parse your own content into it.
Here is the corresponding Controller action:
public ActionResult ShowCodeHtml() { return View("eve-Just.Code.Simple.Html.View"); }
Click here to see the view in action.