29778

Global functions in razor view engine

Question:

I want to have a global method like w in my razor view engine for localization my MVC application. I tried

@functions{ public string w(string message) { return VCBox.Helpers.Localization.w(message); } }

but I should have this in my every razor pages and I don't want that. <strong>I want to know how can I have a global function that can be used in every pages of my project?</strong>

Answer1:

How about an extension method:

namespace System { public static class Extensions { public static string w(this string message) { return VCBox.Helpers.Localization.w(message); } } }

Called like so:

"mymessage".w();

Or:

string mymessage = "mymessage"; mymessage.w();

Or:

Extensions.w("mymessage");

Answer2:

You can extend the HtmlHelper:

Extensions:

public static class HtmlHelperExtensions { public static MvcHtmlString W(this HtmlHelper htmlHelper, string message) { return VCBox.Helpers.Localization.w(message); } }

Cshtml:

@Html.W("message")

Recommend

  • How to elegantly escape single and double quotes when passing C# string to javascript [duplicate]
  • Angular ng If not working with razor syntax
  • Change Page Title in DNN Programatically from Razor
  • Cannot get json data with ajax in Razor Pages [duplicate]
  • MVC 4 Action Result er
  • Generic Method with optional generic parameter
  • Disable Razors default .cshtml handler in a ASP.NET Web Application
  • localizable.strings - works in simulator but not on device
  • .NET Core 1.0 Deployment issue with dotnet-razor-tooling.dll
  • haml, link_to helper, and I18n in the same sentence
  • How to call .cshtml file directly with .cshtml extension from browser without using MVC
  • How to Add new Row Dynamically in ASP.Net MVC 5
  • How to use get all NSLocalization using genstrings while preserved current translations
  • Declare ViewBag on controller
  • Wordpress plugin for dependent drop down lists
  • Opaque reference instead of PImpl. Is it possible?
  • WordPress > setting permalink option via script buggy?
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • Creating a DropDownList
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • JqueryMobile Popup menu is not working
  • NHibernate Validation Localization with S#arp Architecture
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Why winpcap requires both .lib and .dll to run?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • apache spark aggregate function using min value
  • unknown Exception android
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?