HTML encode decode c# MVC4 - c#

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)

Related

How do I render MVC code in a string on my page?

I have an HTML string that also contains a call to a partial view
When I render the string as such, it renders the HTML but not the Partial View.
My Partial file has the text Welcome to my Partial!
#Model.MyText = "This is my Text #Html.Partial('Partials/Partial') Done!";
#Html.Raw(Model.MyText)
Renders:
This is my Text #Html.Partial('Partials/Partial') Done!
Instead of:
This is my Text. Welcome to my Partial! Done!
How would I achieve this?
The razor snippet only works in the context of the .csharp file itself, not the rendered webpage. When you call #Html.Raw(Model.MyText), it correctly renders the string as html. However the resulting html is not parsed as C# code, it is just a string of text. This is not the way to achieve your desired result.
Everything you need done using C# must be done before the render. After rendering, its just html code/text.

SelectPDF missing content after conversion

So I am using SelectPDF to convert an MVC view's html to PDF, but for some reason, a great deal of the content is missing from the PDF.
I have tested the program on other sites I have made in MVC and they work...
So as per the readme doc's recommendation, I use this code to produce a
SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
SelectPdf.PdfDocument doc = converter.ConvertUrl(HTMLPath);
//SelectPdf.PdfDocument doc = converter.ConvertHtmlString(ViewHTML);
doc.Save(PDFSavePath);
doc.Close();
So HTMLSave path is the location of the HTML file created from the rendered MVC view. While ViewHTML contains the HTML in the form of a string.
So this is how the HTML looks (the file and the view look identical, I did double check in case the HTML was captured incorrectly):
But this is how the PDF looks:
Absolutely tragic...
Not sure if it will be helpful, but here's the HTML:
https://drive.google.com/open?id=0B8DiACLG11oYd3p5Tzc2ZlJQLVk
Unfortunately, all the HTML is on one line thanks to the MVC View to HTML
It seems that the html/css engine used to render the page does not support vh, vw for font sizes. It's a common issue with all converters, so probably you should try to change this if you need the page converted to pdf.
Later update: it seems that there is support for vw, but not for vh. That's why the "each" words appear. They use vw in css.

Render Html code with razor

I've searched over this website and I've looked over similar questions and i did not find the answer, I am sure it is somewhere but i did not find it, I have a string like this one for example :
string inputText = "<strong>Hello World</strong>"
This string comes from a certain request in control, and i have no power to change the model or the control. I can only change my razor view, using Html.Raw displays this result :
<strong>Hello World</strong>
And the result i want to be displayed is this one :
Hello World
How is it possible ?
PS: this is only a simple example, it can be any HTML Code.
You should use:
#Html.Raw(HttpUtility.HtmlDecode(inputText))
Decode and then render in html
To render any string (which includes HTML tags) -received from the model- as HTML, use:
#Html.Raw(Model.SomeString)

C# ASP.net render HTML String

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>");

Gracefully handling HTML tags on form submission in asp.net MVC 2

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.

Categories