Webformviewengine based on the Web Form engine and Razorviewengine for the razor engine are subclasses of the abstract type Buildmanagerviewengine, And the latter inherits from Virtualpathproviderviewengine. Here we simply introduce the logic to achieve view acquisition in Razorviewengine. Because view under the razor engine is represented by a Razorview object, and Razorview is constructed from the virtual path of the view file, So Razorviewengine's view acquisition mechanism is to find a view file (. cshtml or. vbhtml file) that matches the specified view name according to the current context, and then according to the The virtual path of the view file creates a Razorview object and is eventually encapsulated as a Viewengineresult object return.
The search for the target view file in Razorviewengine is based on a predefined sequence. If the current request is not for an area, the following list represents the search order for the view:
~/views/{controllername}/{viewname}.cshtml
~/views/{controllername}/{viewname}.vbhtml
~/views/shared/{viewname}.cshtml
~/views/shared/{viewname}.vbhtml
For a request for an area, Razorviewengine searches for the target view in the following order. If the target view file is not successfully found in this list, the search continues with the properties above.
~/areas/{areaname}/views/{controllername}/{viewname}.cshtml
~/areas/{areaname}/views/{controllername}/{viewname}.vbhtml
~/areas/{areaname}/views/shared/{viewname}.cshtml
~/areas/{areaname}/views/shared/{viewname}.vbhtml
If you still find an razorviewengine view file according to the search order above, the list creates and returns a Viewengineresult object based on this listing. The view search mechanism introduced here applies not only to the normal view file, but also to the search for partial view and layout files.
Viewengine not only obtains the specified view from the current context by Findview/findpartialview, but also disposes of the specified view by Releaseview. The implementation of the Releaseview method in Razorviewengine is simple, and if the type of the specified view object implements the IDispose interface, it calls its Dispose method directly. The UML shown in the following illustration shows the related types/interfaces involved in the razor engine and the interrelationships between them.
In the article "ASP.net MVC Razor Engine: Razorview" We created a simplerazorview for simulating razorview, and now we create a corresponding razorviewengine for it, We add the following one simplerazorviewengine directly to the instance project.
1:public class Simplerazorviewengine:iviewengine
2: {
3:private string[] viewlocationformats = new string[] {
4: "~/views/{1}/{0}.cshtml",
5: "~/views/{1}/{0}.vbhtml",
6: "~/views/shared/{0}.cshtml",
7: "~/views/shared/{0}.vbhtml"};
8:private string[] areaviewlocationformats = new string[] {
9: "~/areas/{2}/views/{1}/{0}.cshtml",
Ten: "~/areas/{2}/views/{1}/{0}.vbhtml",
One: "~/areas/{2}/views/shared/{0}.cshtml",
: "~/areas/{2}/views/shared/{0}.vbhtml"};
13:
14:public viewengineresult Findpartialview (controllercontext controllercontext, string partialviewname, bool UseCache )
15: {
16:return Findview (controllercontext, partialviewname, NULL, UseCache);
17:}
18:
19:public viewengineresult Findview (controllercontext controllercontext, String viewName, string mastername, bool use Cache)
20: {
21:string controllername = controllerContext.RouteData.GetRequiredString ("Controller");
22:object AreaName;
23:list<string> viewlocations = new list<string> ();
24:array.foreach (viewlocationformats, Format => viewlocations.add (string. Format (format, ViewName, controllername));
25:if (ControllerContext.RouteData.Values.TryGetValue ("area", out AreaName))
26: {
27:array.foreach (Areaviewlocationformats, Format=>viewlocations.add (string. Format (Format,viewname,controllername, areaname));
28:}
29:foreach (String viewlocation in Viewlocations)
30: {
31:string FilePath = ControllerContext.HttpContext.Request.MapPath (viewlocation);
32:if (File.exists (FilePath))
33: {
34:return New Viewengineresult (New Simplerazorview (viewlocation),
35:this);
36:}
37:}
38:return new Viewengineresult (viewlocations);
39:}
40:
41:public void Releaseview (ControllerContext controllercontext, IView view)
42: {
43:idisposable disposable = view as IDisposable;
44:if (null!= disposable)
45: {
46:disposable. Dispose ();
47:}
48:}
49:}