0

I seem to be hitting a logical contradiction. I have a view with...

</br> in the markup and when I load the page it shows a new line. Inspecting the source I can see a </br>.

Then I put @Html.Raw("</br>") and in the source I get &lt;/br&gt;

However all the documentation says that by default razor will html encode all strings. So why does Html.Raw show an encoded string instead?

Shouldn't it be the other way around?

Exitos
  • 29,230
  • 38
  • 123
  • 178

2 Answers2

0

</br> is incorrect, you probably meant <br/>.

This being said, here's how it works:

<br/> generates <br/>
@("<br/>") generates &lt;br/&gt;
@Html.Raw("<br/>") generates <br/>

The Html.Raw helper is used when you do not want to get HTML encoded output in the resulting HTML. By default the @ Razor function HTML encodes its argument.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • if
    generates
    and
    is html encoded. How come when I go to a html encoding site and enter
    and click encode I get <br/> ? Doesnt make any sense, they are both html encoded?
    – Exitos Jun 03 '13 at 09:14
  • `<br/>` is the HTML encoded representation of the `
    ` string. I don't see what the problem is.
    – Darin Dimitrov Jun 03 '13 at 09:16
  • If I start with
    and html encode it do I end up with
    or <br/>? you say its the html encoded representation what does this mean? To encode something is to change it to another string that can be interpreted in another way. I just cant see why sometimes I end up with
    in the source or <br/>?
    – Exitos Jun 03 '13 at 09:27
-1

From W3schools (I know it ain't W3C official)

Differences Between HTML and XHTML

In HTML, the <br> tag has no end tag. In XHTML, the <br> tag must be properly closed, like this: <br />.

I don't know about razor it's @Html function but you don't need the '/' at all

Brainfeeder
  • 2,604
  • 2
  • 19
  • 37