Encode the html string in Asp.net MVC - asp.net

<%= Html.TextArea("text", "This is <b>Zeb</b>") %>
From the above statement what i expect is to show the string in textbox as:
This is Zeb.
But what i actually get is
This is <b> Zeb </b>
My string is not properly encoded. So please can someone tell me where is the problem?
I have read it from book Professional ASP.NET MVC 4. It's on page 100.

There is no problem, it is expected behavior. Here is a same question: ASP.NET MVC3, Html.TextAreaFor without encoding?

Your question has nothing to do with encoding. You are telling MVC (and the browser) to show a text area to edit that string, and that's what it's doing.
If you want an HTML editor with which your users can edit the HTML (while seeing it properly rendered), you should check this post:
What's the best WYSIWYG editor when using the ASP.NET MVC Framework?
Basically, you'll usually have to include the .js and .css of the selected editor, and activate it on the text area that you are currently sending to the broser. That will create an editor like the one you'll find in Wordpress, for example.
Edit:
Now that you have edited your question, I think you are misunderstanding what the book was referring to. When it says that your text has been encoded it means that the < > characters have been translated into & lt; and & gt; so that the browser doesn't treat them as "open HTML tag" and "close tag".
If you look at the source HTML in your browser, you'll see that the string has been properly encoded (as & lt; and & gt;).

Related

ModX - embedded iframe disappears

I have a YouTube video embedded on a page in ModX. To place it, on the Page I switch TinyMCE off to reveal the HTML code then paste the iframe in. That works Ok but when I go back to edit something else in HTML mode the iframe code disappears.
Anyone know whats going on?
TinyMce is encoding some of the characters in your youtube code for you... it honestly thinks it is being helpful ;) . You have 3 options:
Reference the tinymce documentation, there are some options to tell it which characters not to encode. [sorry, don't remember offhand what they are]
turn off "rich text" on the particular resource you are trying to include the video in.
place your video code in a chunk & reference that chunk in your resource instead of the actual code. [probably your best bet]

Strange characters output on asp.net mvc page

Sometimes (randomly) my asp.net mvc application outputs page where single character is being replaced with weird question marks, as shown on image
This behavior is constant until data changes, and it does not disappear with page refresh. The whole page is in Georgian unicode. I am using asp.net mvc 3 with visual studio 2010 and iis express. This is also observed on published site to IIS 7.5. I have seen similar issues in asp.net webforms application too.
I have observed this behavior not only on db-provided data, but simple anchors, generated with html helper
TagBuilder addAnchor = new TagBuilder("a");
addAnchor.AddCssClass("add-item");
addAnchor.MergeAttribute("href", "#");
addAnchor.SetInnerText(SharedResources.Add); //resource item contains string დამატება
which output anchor "დამატ���ბა"
And this is not issue with page rendering on browser-side, as raw response already contains these symbols.
Update:
As it seems, disabling buffer output by setting Response.BufferOutput = false; removes weird characters from page. But I use Cassette for referencing bundles, and it cannot work with disabled buffer output - no scipts or stylesheets get referenced on page.
Has anyone of you had a similar problem? This is actually critical for me, as currently it affects data where that single character being damaged is the correct answer information of a multi-select test question.
I had the same bug. It is Cassette with a feature that rewrites the result HTML content (and needs BufferOutput set to true) to move referenced script and stylesheet tags to the top if needed.
See the IsHtmlRewritingEnabled option at http://getcassette.net/documentation/configuration.
When I turned off this feature, the bug has disappeared.
My observations:
It is appeared only with remote requests. Local requests are always ok.
The location of question marks is always constant for all clients, browsers and computers.
The location of question marks changes only after recompilation of the application.
I would imagine it's to do with character sets. I've had this issue when i've tried to display a character that isn't in a particular character set.
such as ’ or – (’ or –) in UTF-8 when the character hasn't been HTML encoded previously.
I too had output like this. In my case the source of error was within Views/web.config. An assembly was listed in pages/namespaces, which was not part of the referenced assemblies

FCKEditor replaces & with & when switching from source to design view?

I have the problem that my installed FCKEditor 3.3.1 changes all & to &.
I am posting lots of links in my posts and this makes them invalid. Where can I define that & ist NOT replaced with &?
Thanks :-)
No, you can't. FCKEditor does the right thing.
There is no situation where a standalone & in HTML is not an error. The ampersand always has to be encoded as & - unless it is the start of an escape sequence itself (like in <).
It is a misconception that the & in the HTML source code would make a link invalid. It does not. For HTML to be valid, all data in it must be HTML-escaped. "Data" means both text (in-between tags) and attribute values (like href). When the HTML source is then parsed, the parser will automatically HTML-unescape all data and & will become & again in the DOM. Do not let the fact that many browsers accept an unescaped ampersand deceive you. It is wrong nevertheless (and FCKEditor just tries to deliver valid HTML).
See the custom setting in the fckconfig.js file for FCKeditor.
FCKConfig.ForceSimpleAmpersand = true ;
Tomalak is correct about how it should be presented but I'm just pointing out that there is a configuration option in FCKeditor, in case you need to "break the rules". There are situations where this is necessary. Rendering the ampersand as a true HTML entity (&) does not work in some HTML mail clients, which rarely adhere to HTML standards properly anyway. And you may need to force the plain ampersand if you have an additional filter that will convert it to an HTML entity further along your process.

ASP.NET 3.5 Page character encoding problem

I have a problem in my asp.net 3.5 application (C#) when I try to render in my pages characters like 'è' which are shown in a very strange manner (if i'm lucky i get a ? mark in my web page). in fact Expression Web, when i open my web site, substitutes the è char with �...
How can I tell asp.net that I want to use a particular charset so that i can write in the html source letters like è without using hexadecimal codes??????
I tried in the web.config this:
inside the system.web namespace of the file but nothing works....
Can anyone tell me how to do? THANKS in advance
OK I inserted in every page the element so that everything is ok...
Maybe you can use HttpUtility.UrlEncode or Server.HTMLEn

Response.WriteFile("mymenu.aspx") Bug?

I'm using Response.WriteFile("mymenu.aspx") to write a plain text file out to an area in a MasterPage. Unfortunately it's also printing out an unknown character represented by the square character. The contents of the menu file are as followings:
<ul>
<li>Accounts</li>
</ul>
The square character is what is causing my menu to display incorrectly, pushing it down about 20 pixels. I tested putting just the HTML in the master page instead of including the file and it works fine which mean it must be down to the Response.WriteFile function. I don't suppose anyone else has encountered this problem?
EDIT: I tried the following as well, just to really make sure I wasn't doing anything stupid and that the file didn't contain anything dodgy.
<%
Dim tw As New System.IO.StreamReader(Server.MapPath("menu.aspx"))
For Each s As String In tw.ReadToEnd
Response.Write(s)
Next
%>
It worked. But that still doesn't explain Response.WriteFile behaviour.
Have a look at your text file in an editor that will show all characters including carriage returns, line feeds etc. (Notepad++ is my choice). File may have an unusual eof marker or other encoding issue from the software that created the file.
This sounds like a character encoding issue. Is this file in the same encoding as the page your using it from?
Edit
Open the file up in a hex editor make sure there are no weird characters in there.
I tested your code and it rendered correctly for me.
I have seen ASP.NET alter incoming XML strings in WebMethods before. Save the text to a file on disk before you Response.Write it to compare. Check your CSS for your LI style.

Resources