Search code examples
asp.net-mvclocalizationhtml-helpernopcommerce

creating html helper equavalent to nopcomerce @T() helper


I was working on NopCommerce, and it has an HTML helper @T("") that takes a string key and fetches its value from the database.

I want to implement this in my project. I googled a lot but didn't find any help on how they created a helper method like this.

Can somebody help me creating similar helper like this?


Solution

  • You need to tell razor to use your own WebViewPage. You declare this inside the web.config file under Views folder. Your custom WebViewPage must be specified in the pageBaseType attribute of the pages element. All the cshtml implementations will inherit your custom WebViewPage and you can access the pulic or protected properties and method of your custom WebViewPage.

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, 
                     Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="Custom.MyCustomWebViewPage">
    </system.web.webPages.razor>