2

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 ?

2 Answers 2

2

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 + "\"") />
}
Sign up to request clarification or add additional context in comments.

2 Comments

I´m still getting the same problem, in my layout page I use <meta charset="utf-8"> and after that @RenderSection("meta", required: false), does this have something to do ?
@Steve - Can you see what happens if you put the @Html.Raw(Model.title) directly in the page somewhere (i.e. not in the meta tag). If it works correctly there, you may be facing the same problem as here, in which case you could work around it by using the slightly uglier: <meta name="title" @Html.Raw("content=\" + Model.title + "\"") />
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.