Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global functions in razor view engine

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. I want to know how can I have a global function that can be used in every pages of my project?

like image 233
ahmadali shafiee Avatar asked Oct 23 '25 17:10

ahmadali shafiee


1 Answers

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")
like image 137
andres descalzo Avatar answered Oct 25 '25 07:10

andres descalzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!