Encoding issue asp.net mvc 4 - asp.net-mvc

I´m trying to render a dynamic database title in asp.net mvc view. So I have something like this in my view.
#section meta {
<meta name="title" content="#Model.title" />
}
When model has special characters like Misión in spanish it shows in title something like
Misi&#243;n ... I´m using meta charset utf8 in my layout. Is there a special encoding I´m missing ?
How can I render Misión in title page ?

Using #someproperty will assume you're rendering out HTML and make sure it gets encoded to prevent things like cross site scripting. In this instance you want it to render the raw value, in which case you need to use Html.Raw(...) to render your content in it's raw form.
#section meta {
<meta name="title" content="#Html.Raw(Model.title)" />
}
However, just be aware that if the Model.title can come from user generated content (or some other untrusted source), you could be opening yourself up to security issues (for example if your Model.title's value was "test" /> <script ...etc...", a malicious user could use it to inject code into your pages.
Edit: Just including the content of my comment below for future googlers, since it appears that was the actual solution...
If you put the #Html.Raw(Model.title) directly in the page somewhere (i.e. not in the meta tag) and it works correctly there, you may be facing the same problem discussed here, in which case you could work around it by using the slightly uglier:
#section meta {
<meta name="title" #Html.Raw("content=\" + Model.title + "\"") />
}

Approach - 1
string value1 = "<html>"; // <html>
string value2 = HttpUtility.HtmlDecode(value1); // <html> //While getting
string value3 = HttpUtility.HtmlEncode(value2); // <html> //While saving
Approach - 2
Html.Raw("PKKG StackOverFlow"); // PKKG StackOverFlow

Related

ASP.NET MVC 5 - redirect from view only once

Hy,
I'm trying to redirect users which has deactivated javascript with following code in the main layout (_Layout.cshtml):
<noscript>
<meta http-equiv="refresh" content="0;url=/Error/NoJavascript" />
</noscript>
Rather it works, but it refreshes every time. How can I redirect only once instead of every time?
I've found this code sample, but it doesn't let me compile (error message: can't implicite convert from void to object) I use this code sample in my view (_Layout.cshtml):
#Response.Redirect("/Error/NoJavascript")
Thanks for help :)
Use a simple static HTML file for this special case which has not this meta data:
<meta http-equiv="refresh" content="0;url=#Url.Content("~/Content/NoScript.html")" />
Your returned view from the Error/NoJavascript action method has a reference to the layout page automatically or just set its layout to null.
#{
Layout = null;
}

I am trying to display indented XML within a MVC3 View, and I cannot get it to work, it just appears non indented?

I have been trying to use the XElement, as advised by John Saunders. However My XML is not indented in the Razor View. I must be doing something silly, but I cannot see it.
Controller Code:
XElement myXElement = XElement.Load(strMapPath + strFileName);
ViewBag.MyOrigDocXML = myXElement;
return View();
Razor Code in View:
#if(ViewBag.MyOrigDocXML != null)
{
#ViewBag.MyOrigDocXML.ToString();
}
Any help greatly appreciated,
I would nest your code inside pre html tags like
<link href="~/Content/Prettify/prettify.css" rel="stylesheet" />
<script src="~/Scripts/Prettify/prettify.js"></script>
<body onload="prettyPrint()">
<pre class="prettyprint lang-xml">#ViewBag.MyOrigDocXML;</pre>
</body>
this will then give you your xml in an indented layout, then add prettify to get xml text highlighted

C# MVC Textbox MultiLanguage

I have a view with a TextBox for input. However it seems like it doesn't support all Spanish characters. The upside down question mark doesn't seem to work. Is there a simple way to get around this?
<%= Html.TextBoxFor(m => m.Product.Name, new { style = "width:400px", maxlength = 150 })%>
Things to check:
Inside your web.config you are using utf-8:
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
Inside the <head> section of your site you have a <meta> tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Your view files are utf-8 encoded with BOM on the disk

How to get at Header

I'm trying to pass the header object here:
<%=FileUtil.AddStylesheetToHeader(Header, "UsedItems.css") %>
In my Master page I have a <head runat="server">. And my aspx page definitely has a reference to my MasterPageFile in the page directive at the top of my MVC based .aspx.
I also have an import statement the namespace that the FileUtil class resides in :
<%# Import Namespace="xxxx.Web.Utilities" %>
In standard ASP.NET you could reference the header with this.Header but in MVC I'm not able to do this...or I'm missing some kind of Imports or something.
for some reason though at runtime, with that call to AddStylesheetToHeader, I get the following error:
The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments.
I'm not sure why it's looking at a .NET type as I know when I mouseover my FileUtil at compile time it's definitely referencing xxxx.Web.Utilities.FileUtil.
In that method I'm using HtmlLink styleSheet = new HtmlLink(); I may not be able to use this as it's an ASP.NET Web control? Here's that method:
public static void AddStylesheetToHeader(HtmlHead header, string cssFilename)
{
HtmlLink styleSheet = new HtmlLink();
styleSheet.Href = "content/css/" + cssFilename;
styleSheet.Attributes.Add("rel", "stylesheet");
styleSheet.Attributes.Add("type", "text/css");
header.Controls.Add(styleSheet);
}
I don't think I can use conrols that stem from System.Web.Controls since this is an ASP.NET application? If so, the how can I add a control to the header controls collection? Would I need to do this differently in MVC?
There may be a way to do it the way you're attempting, but it's more common in ASP.NET MVC to create a content placeholder in the <head> rather than accessing it programmatically. For example, your master view could look something like this:
<html>
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
</html>
And your view could look like this:
<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
<link href="/content/css/UsedItems.css" rel="Stylesheet" type="text/css" />
</asp:Content>
have you tried this.Request.Header?
You can use JavaScript to dynamically add content to your HEAD section as shown in the code below:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("head").append("<link href='Content/Site.css' rel='stylesheet' type='text/css' />");
});
</script>

Grails interprets and closes HTML meta tag

In my Grails GSP file I'm using the HTML meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The problem is that Grails closes this tag and renders it as:
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
This fails W3C's HTML validation (since my doctype is HTML and not XHTML).
Is there a fix for this? How can I get Grails to not interpret the
meta tag?
I'm using grails-1.2-M4.
Follow up:
I create the Grails bug GRAILS-5696 for this issue.
Not sure that this is the most beautiful solution, but at least it will work for your case:
<%= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' %>
Well...this does not work since it is preprocessed by Grails before displayed as is.
So the only solution I see is to create a TagLib and output the content like this:
class MetaTagLib {
static namespace = 'my'
def meta = {
out << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
}
}
and use it like:
<my:meta />
It works. Tested.
You could validate as HTML5 instead of HTML 4.01, by using <!DOCTYPE html> (that's it, really!). HTML5 allows trailing slashes even in the HTML syntax, in order to allow for systems like this that produce pseudo-XHTML.
Of course, HTML5 is not yet a finished standard; it may change. I think that this aspect of it is unlikely to be changed, but there is still some fairly contentious debate about a lot of the new HTML5 features, so keep in mind that it's not yet finalized.

Resources