How to implement free api in ASP.NET MVC?

I want to inject free api into my mvc sites. I got the basics. So, implement an object library, for example:

public class UIElement{/*...*/} public class ButtonBase : UIElement{/*...*/} public class LinkButton : ButtonBase {/*...*/} public static class Extensions { public static T UIElementMethod<T>(this T element, string title) where T : UIElement { return element; } public static T ButtonBaseMethod<T>(this T element, string title) where T : ButtonBase { return element; } public static T LinkButtonMethod<T>(this T element, string title) where T : LinkButton { return element; } } 

But how to use it in razor mode without calling the flush method.

 @Html.UIproject().LinkButton() .UIElementMethod("asd") .ButtonBaseMethod("asd") .LinkButtonMethod("asd") 

But it returns the name of the class. I tried to make an implicit statement for MvcHtmlString, but it did not call. Any idea how to achieve this. How to find out about the chain. I love the way the Kendo user interface works.

Thanks,
Peter

+6
source share
2 answers

In UIElement classes, you must implement the IHtmlString interface. This interface ToHtmlString method is called by Razor and should return a string encoded in HTML.

So, I would implement this based on the UIElement abstraction and create a RenderHtml method that can be implemented by specific LinkButton , etc. classes:

 public abstract class UIElement : IHtmlString { public string ToHtmlString() { return RenderHtml(); // This should return an HTML-encoded string. } public override string ToString() { return ToHtmlString(); } protected abstract string RenderHtml(); } 

If you check KendoUI in Reflector / JustDecompile / dotPeek in the WidgetBase class, you will see the same template.

+4
source

I have not tried this in this particular situation, but you could use an implicit cast to convert from a free constructor to the object you need (see this blog ).

0
source

All Articles