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:
  1. HTML markup that is usually stored as an embedded file, thus available by the embedded file system.
  2. An optional view class, which gives the availability to manipulate the HTML string further on the server side.
The view name can refer to either an embedded html file, or a view class. In the above example we refer to an embedded html file. Here is the breakdown of the name for tis example:
  1. "eve-": is the ViewEngine prefix, so essentially, we are explicitly asking this view to be processed by the ViewEngine we registered earlier.
  2. "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.
  3. "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.
  1. Class derived from EmbeddedView

    Inheriting from EmbeddedView class ensure that the class has the ProcessView method that enables further processing of the HTML.

  2. 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.
  3. 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.
  4. 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.
The coresponding controller implementaiton:
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.