How I can create an extension method like “Html.DisplayTextFor” in Asp.net mvc ? I mean I need an extension for my page class in asp.net like “Page.DisplayTextFor” how I can implement it. I have created this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.UI
{
public static class PageExtension
{
public static void DisplayTextFor(this string text)
{
/*some operation*/
}
}
}
but it doesn’t work.
I assume you mean you want to extend the Page class adding your own functions to appear when using
this.Page...?If so, first have the
PageExtensionclass as part of your own namespace. Then the syntax is:The extension method first parameter defines what class to extend, the rest of the parameters define what you send when calling it.
To call the above from within your page, just have: