The previous article introduced the implementation of dynamic custom routing in MVC , this article describes three ways to call the background method in the Razor view to output page code in a global way.
Framework of the latest upgrade to implement a page part function, in fact, through the background method query database content, the query results of the HTML code into the Razor view, considering the flexibility, you need to be able to call the method in any Razor view, so arbitrary Razor Pages can easily share the HTML content of the page part in a uniform way, which is necessary for the reusability and maintainability of the code.
To achieve the above requirements, this article describes the following three ways to choose from.
1, extend the static class Helper method, return htmlstring
1) refer to the following code:
public static class imagehelper{public static htmlstring Image (This HtmlHelper Helper, string id, string URL, string alternatetext) {return Image (helper, id, URL, alternatetext, null); public static htmlstring Image (This htmlhelper helper, string id, string URL, String AlternateText, Object Htmlattrib Utes) {//Instantiate a urlhelper var urlhelper = new Urlhelper (helper. Viewcontext.requestcontext); Create Tag Builder var builder = new Tagbuilder ("img"); Create Valid ID builder. Generateid (ID); Add attributes Builder. Mergeattribute ("src", urlhelper.content (URL)); Builder. Mergeattribute ("alt", AlternateText); Builder. Mergeattributes (New RouteValueDictionary (htmlattributes)); Render tag var ret = new Mvchtmlstring (builder. ToString (tagrendermode.selfclosing)); return ret; }}
2) At this point the interface can be called:
@Html. Image ("My-id", "~/content/my-img.png", "Alt Text")
Summary: This is the simplest, but the disadvantage is also obvious, because it is a static class, it is not convenient to make dependency injection to invoke other instance methods.
2, inherit webviewpage<tmodel> implement the custom Webviewpage, implement the method of returning interface HTML string in subclass.
This approach can be used to refer to the localization implementation process for multiple languages in the ABP framework, as follows:
1) inheriting the Webviewpage class
Public abstract class Abpwebviewpage<tmodel>: webviewpage<tmodel>
2) Implementation method that returns the localized language string for the specified Key name
<summary>///Gets localized string for given key name and current language.///</summary>///<param nam E= "Name" >key name</param>///<returns>///localized string///</returns>protected virtual string L (string name) { return this._localizationsource.getstring (name);}
3) Configuring the View EOG class in Web. config
<system.web.webpages.razor>
4) It is convenient to call @l ("Dashboard") code on this view page to output the string contents of the corresponding language
Summary: This approach requires that the base class for the page view be configured in Web. config under the specified folder, and that methods cannot be called on the page that is not configured
3, recommended the best solution
We implement the helper method in a different way by using the helper code in the global Razor view to access the background method and output the results
1) Inherit the System.Web.WebPages.HelperPage and override the Html property
It is important to note here that the HTML object of System.Web.WebPages.HelperPage is not the same class as the HTML object that is rewritten below, for the purpose of remembering and unifying the interface calls in the MVC Razor keyword, we define the property name as Html.
public class helperpage:system.web.webpages.helperpage{ //workaround-exposes the MVC HtmlHelper instead of the No Rmal Helper public static new HtmlHelper Html { get {return (webviewpage) WebPageContext.Current.Page). Html; } }}
2) put the Helper method in the Razor view under the App_Code folder
We already know that Razor can access the helper methods defined in the other Razor views under this path, but now we are considering that the helper method can be shared by any path under Razor view.
First, create a Razor view under the App_Code folder, which will be dynamically compiled with the HTML code required to invoke the background method output interface through the Razor view engine, with the following code:
@inherits ybrapidsolution.mvc.helperpage@using system.web.mvc.html@helper Partial (string id) { Html.renderaction ("_widget", "Home", new {ID});
3) We access the database in the backend method of _widget, then generate a partial view of the page and return to the interface with the following code:
#region CMS Parts rendered///<summary>///CMS parts rendered//</summary>///<param name= "id" ></param>///< Returns></returns>[childactiononly]public Partialviewresult _widget (string id) {if (string. IsNullOrEmpty (ID)) {return Partialview ("E404"); } var widget = _widgetservice.getbyid (id); if (widget = = null) {return Partialview ("E404"); } if (string. Isnullorwhitespace (widget. TemplatePath) {return Partialview ("_widget", Widget); } return Partialview (widget. TemplatePath, widgets);} #endregion
4) This allows the following code to be called on any page to generate the Html string required for the interface, and the contents of the string can be placed in the database, and can be modified and maintained in the background when needed:
@_widget.partial ("31DBFB04B41E4883BAB880CEEC2CFEF3")
Summary: This approach enables global sharing of Helper methods without the need for additional configuration, and allows you to use your own defined tags when calling, which makes the code more readable.
Ybsoftwarefactory code Generation Plug-in "25": three ways to call a background method in a Razor view to output page code in a global way