NopCommerce Architecture Analysis of of Six Custom RazorViewEngine and WebViewPage

  • 2021-07-16 02:15:46
  • OfStack

The support for Razor in the system consists of two parts, one of which is custom RazorViewEngine

1. Customize RazorViewEngine

In the Application_Start method of Global. asax. cs, the custom view engine is registered:


//remove all view engines 
ViewEngines.Engines.Clear(); 
//except the themeable razor view engine we use 
ViewEngines.Engines.Add(new ThemeableRazorViewEngine());

ThemeableRazorViewEngine inherits ThemeableBuildManagerViewEngine,

ThemeableBuildManagerViewEngine inherits ThemeableVirtualPathProviderViewEngine

ThemeableVirtualPathProviderViewEngine inherits VirtualPathProviderViewEngine to parse the virtual path.

When it comes to the view engine (ViewEngine) in ASP. NET MVC, we have to say that IView and IViewEngine are two interfaces, which must be implemented to implement a custom view engine:

IView interface: IView is an abstraction of View object in MVC structure, and this interface has only one method: void Render (ViewContext viewContext, TextWriter writer); The Render method writes the page HTML into the Writer for the browser to display;

IViewEngine interface: The IViewEngine interface is responsible for finding View objects. When writing your own view engine, you can inherit from IViewEngine and override the FindView and FindPartialView methods of this class, which return 1 ViewEngineResult to represent search results.

ASP. NET MVC provides two classes that implement the IViewEngine interface: VirtualPathProviderViewEngine and WebFormViewEngine. The VirtualPathProviderViewEngine class implements two methods, FindView and FindPartialView, to search the page file according to the specified path and format, and provides an Cache mechanism to cache data (it cannot be used in WebService or WCF projects because it uses ASP. NET Cache and depends on HttpContext).

When VirtualPathProviderViewEngine looks for pages, the specific paths to look for are actually determined according to these three attributes in this class: MasterLocationFormats, ViewLocationFormats and PartialViewLocationFormats. Modifying these three attributes can assign custom search paths and file formats to our view engine.

2. Custom class WebViewPage < TModel >

This class represents the properties and methods required to render a view using ASP. NET Razor syntax.

So every 1 view should inherit this class. However, we do not see this inheritance in the project, and Razor inherits the view from System. Web. Mvc. WebViewPage by default < TModel > Base class. You can also change the default base class by modifying the web. config file under the view directory (there is one ~/Views/directory under every asp. net mvc project), which is how NopCommerce implements custom WebViewPage classes. In addition, namespaces can be introduced into the view file, but this approach is cumbersome, unless there are individual views in a project that need to customize WebViewPage.


Related articles: