I have a bit of code in my markup like this :
<% Response.Write(((String)objRow["Post"]).Trim()); %>
The value in objRow["Post"] is HTML markup - similar to this:
<p><span style="color: #ff0000;">asdsada</span></p>
The content of which was created using tinymce. I now want to render the resultant html - basically a view function of a previously created save function.
At the moment, my markup literally spits out the HTML you see there onto my website - but what I really want is a red line of text saying asdsada.
Please help
it sounds like the value you are trying to display is already html encoded. try this:
<%= HttpUtility.HtmlDecode((string)objRow["Post"]) %>
My version of Asp.net is little bit higher, but this might help someone, who uses .net Core 2.0 MVC.
I am using the following approach for rendering html in my Asp.Net Core 2.0 MVC project (the html is created via Tiny MCE and saved in the database, after that it is rendered on the Razor View).
Please, try these two steps:
1) include System.Net in _ViewImports.cshtml:
#using System.Net
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2) render the html using HtmlDecode in your View.cshtml:
#Html.Raw(#WebUtility.HtmlDecode(Model.MyHtmlData))
This works for me, hope it helps!
But without HtmlDecode, Html.Raw displayed MyHtmlData with html tags (didn't render html, just displayed it). I guess, that is because the data was HTML encoded already, as Zdravko Danev mentioned:
#Html.Raw(Model.MyHtmlData)
You can use literal control in asp.net to display the text and it will render it accordingly.
literal1.text = htmlcodeuwanttodisplay
Did you try something like
Server.HtmlEncode("<p><span style='color: #ff0000;'>asdsada</span></p>");
Related
I am using ASP.NET MVC, when I want to use the tag in #Html.Raw, this tag does not appear in the desired <div>.
As shown here:
<div class="mt-4 current-cursor">
#Html.Raw("<strong>OKK</strong> <p><ul><li style='font-size:18px;'>1.Test1</li><li>2.Test2</li></p>")
</div>
The result that it displays for me is as below, that is, it does not recognize the <strong> tag at all.
Html.Raw does not interpret anything at all. It just spews the given string unencoded into the output docuument.
So if it doesn't look right in your case, possible you have some CSS in that page that causes it to look as it does. You could use F12 (Developer Tools, depending on your browser) to inspect the "OKK" for details.
BTW, the other tags in your example also look wrong (which could also be an issue given existing CSS in the page).
In my case, for example, using some (other) arbitrary styles, your code looks like this:
There seems to be a problem with the ASP textbox control when set to multiline and serving the page as xhtml. The project I am working on uses content negotiation to serve asp pages as application/xhtml+xml to browsers which support it. The problem is when asp textbox renders a textarea to the page, it explicitly prepends a newline to the text. Reflection of the textbox's render method looks like the following:
if (TextMode == TextBoxMode.MultiLine)
HttpUtility.HtmlEncode(Environment.NewLine + this.Text, (TextWriter) writer);
When firefox and opera are served this with xhtml content type, they interpret the newline as part of the text in the textarea and so I get extra newlines at the beginning of my text areas.
I could subclass textbox and override render, but that seems like a bit of overkill to correct something like this. Is there another way to correct this? And does anyone know why asp textbox does this anyway?
An alternative to subclassing is to use control adapters, or to write the <textarea by hand and get ASP.NET to generate the control name attribute for you.
I suspect ASP.NET WebForms does this simply because of a short-sight. The future is MVC anyway, so don't expect this to be changed any time soon. I suspect the original purpose of the newline is to give the textarea a "value" rather than nothing (thus making the textarea "successful" in HTML forms parlance).
This isn't the only odd behaviour you'll see in ASP.NET. The atrocious HTML formatting of <head runat="server"> is also on the list.
Iam using wkhtmltopdf for html to pdf conversion of an html page using C# code and it is working absolutely perfect but i want to convert a particular part of html page to pdf like by specifying div id of that part or any similar method.How can i do this?
Please help
You can just create a new HTML page with just your div as a body:
<html>
<head><title>...</title></head>
<body>
<!-- PLACE YOUR DIV HERE -->
</body>
</html>
Since you already know how to produce a PDF from an HTML file, you know know how to produce a PDF from a DIV ;)
BUT: Of course the CSS is going to be all wonky, since the selectors won't match anymore etc. This is a problem for the general case, but I'm guessing in your specific case, you can make that work.
I am in a process of upgrading a c# MVC2 project into c# MVC4.
Here is the scenario in MVC2
Input string(from database)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view)
rendered using
<%=Model.text %>
Hi
hello!
you there
Here is the scenario in MVC4
Input string(from database)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view)
rendered using
#Model.text
<p>Hi<br>hello!<br>you there</p>
I tried
#HttpUtility.HtmlDecode(Model.text)
#HttpUtility.HtmlEncode(Model.text)
Nothing helps...
I had a similar problem in MVC4 asked here (the ajax result is rendered with html tags not the actual html)
Is some of my settings troubling me??? or is that something to do with HTML 5 or am I missing anything in using MVC4.
Please help!!
This should do the trick:
#Html.Raw(Model.text)
If you don't want your text get encoded, that text should be of type IHtmlString. String texts are encoded by default.
In your case,
Model.text = MvcHtmlString.Create("<p>Hi<br>hello!<br>you there</p>");
would do the trick as well.
In controller side
viewbag.msg="hello";
in the html.cs razor view
#Html.Raw(viewbag.msg)
Hi I have a MVC site and will have users inputting text to be displayed on a web page.
I am trying to come up with a graceful way of handling any HTML that the user try's to input - as you will probably be aware MVC 2 throws an error for any HTML in the text.
I use ViewModels and decorate my properties with filters from the DataAnotation class to validate my forms.
Anybody now of such a way?
Is there some crazy regex that will NOT match HTML but anything else or some other way?
I am open to any suggestions.
Thanks,
Simon
Adding the following attribute will stop the runtime from complaining:
[ValidateInput(false)]
public ActionResult SomeEvilAction ()
{
/* ... */
}
Now it's your task to HTML encode every input you display back on a page:
<%= HttpUtility.HtmlEncode (Model.Text) %>
or
<%: Model.Text %>
I did this exact thing on a site I did the other day.
I am using a WYSIWYG editor that puts in proper html, not bb code.
I disabled validation on the page from the page directive to stop mvc throwing the potentially unsafe code exception and removed all instance of scripts tags using regex.
See Developer Art's post
You may need to add this to your web.config
<httpRuntime requestValidationMode="2.0" />
The regex I used is as follows:
(?<startTag><\s*script[^>]*>)(?<scriptContent>[\s\S]*?)(?<endTag><\s*/script>)
This will give you 3 named groups.
startTag
scriptContent
endTag
So you can do a replace on the script element and show the content of the script, or remove it altogether.
Anything you wish to do really.