Steve
Steve

Reputation: 959

Encoding issue asp.net mvc 4

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&amp;#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 ?

Upvotes: 2

Views: 873

Answers (2)

Alconja
Alconja

Reputation: 14883

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 + "\"") />
}

Upvotes: 2

Chamaququm
Chamaququm

Reputation: 6728

Approach - 1

string value1 = "&lt;html&gt;";                  // &lt;html&gt;
string value2 = HttpUtility.HtmlDecode(value1);  // <html>        //While getting
string value3 = HttpUtility.HtmlEncode(value2);  // &lt;html&gt;  //While saving

Approach - 2

Html.Raw("PKKG &nbsp; StackOverFlow");           // PKKG StackOverFlow

Upvotes: 0

Related Questions