We can create our own custom view engine in MVC. To create our own custom view engine we need to follow three steps:
Let’ say we want to create a custom view engine where in the user can type a command like “<DateTime>” and it should display the current date and time.
: We need to create a class which implements the interface. In this class we should write the logic of how the view will be rendered in the function. Below is a simple code snippet for that.
: We need to create a class which inherits from and in this class we need to provide the folder path and the extension of the view name. For instance, for Razor the extension is “cshtml”; for aspx, the view extension is “.aspx”, so in the same way for our custom view, we need to provide an extension. Below is how the code looks like. You can see the is set to the folder and the extension is “ ”.
: We need to register the view in the custom view collection. The best place to register the custom view engine in the collection is the file. Below is the code snippet for that.
Below is a simple output of the custom view written using the commands defined at the top.
If you invoke this view, you should see the following output: