Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Custom HtmlHelpers for MVC3 and MVC4 Beta, should I use HtmlString or MvcHtmlString?

I'm writing some custom Html Helpers for my MVC3 and soon MVC4 project. Examples on the net usually show that it's done with MvcHtmlString, however, I read that if we are using .NET 4, we should be using HtmlString.

What should I use and why?

Thanks.

like image 450
Saxman Avatar asked Jan 30 '26 14:01

Saxman


1 Answers

Always use the highest possible interface/class in the hierarchy when designing something. In this case it's IHtmlString and you could use it in ASP.NET MVC 3 and 4.

public IHtmlString Foo(this HtmlHelper helper)
{
    return new HtmlString("foo bar");
}

Be careful when returning an IHtmlString instance from your helper. This means that it is up to you to properly HTML encode it:

like image 71
Darin Dimitrov Avatar answered Feb 01 '26 08:02

Darin Dimitrov