In the [design article], we describe the principle of creating from viewresult to view rendering through a general introduction to the view engine, in order to give readers a deep understanding of the view engine and its view presentation mechanism, We have customized a simple staticfileviewengine for rendering static HTML. In an empty Web application created from the ASP.net MVC project template in Visual Studio, we define a custom Staticfileview for rendering static HTML content. Staticfileview implements the IView interface and reads the contents of the developed file into the TextWriter of the parameters in the implemented Render method.
1:public class Staticfileview:iview
2: {
3: Public string FileName {get; private set;}
4: Public Staticfileview (string fileName)
5: {
6: This . filename = filename;
7: }
8: Public void Render (ViewContext viewcontext, TextWriter writer)
9: {
Ten: byte[] buffer;
One: using (FileStream fs = new FileStream (this. FileName, FileMode.Open))
: {
buffer = new Byte[fs. Length];
FS. Read (buffer, 0, buffer.) Length);
: }
: writer. Write (Encoding.UTF8.GetString (buffer));
: }
18:}
Because the content defined in Staticfileview is completely static, caching appears to be necessary. We simply need to cache the view based on the controller and view names, and we define the following as the data type Viewengineresultcachekey for the key.
1:internal class Viewengineresultcachekey
2: {
3:public string controllername {get; private se T
4:public string ViewName {get; private set;}
5:
6:public viewengineresultcachekey (String controllername, String viewName)
7: {
8: This. Controllername = controllername?? String. Empty;
9:this. ViewName = ViewName?? String. Empty;
10:}
11:public Override int GetHashCode ()
: {
13:return this. Controllername.tolower (). GetHashCode () ^ This. Viewname.tolower (). GetHashCode ();
14:}
:
16:public override bool Equals (object obj)
: {
18:viewengineresultcachekey ke y = obj as viewengineresultcachekey;
19:if (Null = = key)
: {
21:return false;
:}
23:return key. GetHashCode () = = this. GetHashCode ();
24:}
:}
Staticfileviewengine that have the following definition represent the Staticfileview corresponding viewengine. We use a dictionary-type field viewengineresults as a cache of Viewengineresult, and the capture of the view is finally implemented in the Internalfindview method. View definition through Staticfileview is defined in a text file with a view name as the file name with the extension. shtml (Static HTML).
1:public class Staticfileviewengine:iviewengine
2: {
3:private Dictionary<viewengineresultcachekey, viewengineresult> viewengineresults = new Dictionary<ViewEng Ineresultcachekey, viewengineresult> ();
4:private Object synchelper = new Object ();
5:public viewengineresult Findpartialview (controllercontext controllercontext, string partialviewname, bool UseCache)
6: {
7:return this. Findview (ControllerContext, partialviewname, NULL, UseCache);
8:}
9:
10:public viewengineresult Findview (controllercontext controllercontext, String viewName, string mastername, bool use Cache)
11: {
12:string controllername = controllerContext.RouteData.GetRequiredString ("Controller");
13:viewengineresultcachekey key = new Viewengineresultcachekey (controllername, viewName);
14:viewengineresult result;
15:if (!usecache)
16: {
17:result = Internalfindview (ControllerContext, ViewName, controllername);
18:viewengineresults[key] = result;
19:return result;
20:}
21:if (Viewengineresults.trygetvalue (key, out result))
22: {
23:return result;
24:}
25:lock (Synchelper)
26: {
27:if (Viewengineresults.trygetvalue (key, out result))
28: {
29:return result;
30:}
31:
32:result = Internalfindview (ControllerContext, ViewName, controllername);
33:viewengineresults[key] = result;
34:return result;
35:}
36:}
37:
38:private viewengineresult Internalfindview (controllercontext controllercontext, String viewName, String ControllerN Ame
39: {
40:string[] searchlocations = new string[]
41: {
42:string. Format ("~/views/{0}/{1}.shtml", Controllername, ViewName),
43:string. Format ("~/views/shared/{0}.shtml", ViewName)
44:};
45:
46:string fileName = ControllerContext.HttpContext.Request.MapPath (Searchlocations[0]);
47:if (File.exists (fileName))
48: {
49:return new Viewengineresult (FileName), this);
50:}
51:filename = string. Format (@ "\views\shared\{0}.shtml", viewName);
52:if (File.exists (fileName))
53: {
54:return new Viewengineresult (FileName), this);
55:}
56:return new Viewengineresult (searchlocations);
57:}
58:
59:public void Releaseview (ControllerContext controllercontext, IView view)
60: {}
61:}