The MSDN documentation is correct, but maybe a little confusing. The MvcHtmlString and IHtmlString are used to represent a string that has already been encoded in HTML format. MSDN says:
Returns an HTML encoded string that represents the current object.
The object you passed to the MvcHtmlString object MvcHtmlString already been encoded in HTML, so both .ToString() and .ToHtmlString() just return the object you passed.
Please note that the MSDN docs clearly state that:
The ToHtmlString and ToString methods return the same value.
So why all this? Two reasons:
- In the Razor view engine and in ASP.NET Web Forms v4, an object that implements
IHtmlString is written as raw data. Viewers suggest that the person creating the IHtmlString has already processed the data. IHtmlString has its own stringify method, so it should not have the same implementation as ToString() . While ToHtmlString() should return HTML, you can easily imagine that ToString() can return some debugging information other than the developer.
Eilon
source share