Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize RazorViewEngine and WebViewPage in NopCommerce

Shulou Source: shulou.com Published: 2022-06-03 13:02:13 09月30日 Update

This article will explain in detail how to customize RazorViewEngine and WebViewPage in NopCommerce. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Custom 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 achieve the purpose of parsing virtual paths.

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

IView interface: IView is an abstraction of View objects in MVC structure. There is only one method for this interface: void Render (ViewContext viewContext, TextWriter writer). The Render method writes the page HTML to Writer for browser 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 class's FindView and FindPartialView methods, which return a ViewEngineResult to represent the 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 for page files based on the specified path and format, and provides a Cache mechanism to cache data (because you are using ASP.NET Cache and dependent on HttpContext, it cannot be used in WebService or WCF projects).

When VirtualPathProviderViewEngine looks for a page, the specific path from which to find is actually based on the three attributes in the class: MasterLocationFormats, ViewLocationFormats, PartialViewLocationFormats. Modify these three properties to assign a custom search path and file format to our view engine.

2. Custom class WebViewPage

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

So every view should inherit this class. But we don't see this inheritance in the project, and Razor inherits the view from the System.Web.Mvc.WebViewPage base class by default. You can also change the default base class by modifying the web.config file under the view directory (there is a ~ / Views/ directory under each asp.net mvc project), and NopCommerce uses this method to implement custom WebViewPage classes. In addition, namespaces can be introduced into view files, but this approach is cumbersome unless there are individual views in a project that need to be customized WebViewPage.

This is the end of this article on "how to customize RazorViewEngine and WebViewPage in NopCommerce". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Views methods interfaces two engines files paths projects properties articles pages search three objects more formats directories good practical tedious Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi MySQL Shulou Tech Info macOS