Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:17:20+00:00 2026-05-29T09:17:20+00:00

In our MVC3 ASP.net project, the HttpUtility.HtmlEncode method seems to be escaping too much

  • 0

In our MVC3 ASP.net project, the HttpUtility.HtmlEncode method seems to be escaping too much characters. Our web pages are served as UTF-8 pages, but still the method escapes characters like ü or the Yen character ¥, even though tese characters are part of the UTF-8 set.

So when my asp.net MVC view contains the following piece of code:

    @("<strong>ümlaut</strong>")

Then I would expect the Encoder to escape the html tags, but not the ümlaut

    &lt;strong&gt;ümlaut&lt;/strong&gt;

But instead it is giving me the following piece of HTML:

    &lt;strong&gt;&#252;mlaut&lt;/strong&gt;

For completeness, I also mention that the responseEncoding in the web.config is explictely set to utf-8, so I would expect the HtmlEncode method to respect this setting.

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:17:21+00:00Added an answer on May 29, 2026 at 9:17 am

    As Aristos suggested we could use the AntiXSS library from Microsoft. It contains a UnicodeCharacterEncoder that behaves as you would expect.

    But because we

    • didn’t really want to depend on a 3rd party library just for HTML Encoding
    • were quite sure that our content didn’t exceed the UTF-8 range.

    We chose to implement our own very basic HTML encoder. You can find the code below. Please feel free to adapt/comment/improve if you see any issues.

    public static class HtmlEncoder
    {
        private static IDictionary<char, string> toEscape = new Dictionary<char, string>()
                                                                {
                                                                    { '<', "lt" },
                                                                    { '>', "gt" },
                                                                    { '"', "quot" },
                                                                    { '&', "amp" },
                                                                    { '\'', "#39" },
                                                                };
        /// <summary>
        /// HTML-Encodes the provided value
        /// </summary>
        /// <param name="value">object to encode</param>
        /// <returns>An HTML-encoded string representing the provided value.</returns>
        public static string Encode(object value)
        {
            if (value == null)
                return string.Empty;
    
            // If value is bare HTML, we expect it to be encoded already
            if (value is IHtmlString)
                return value.ToString();
    
            string toEncode = value.ToString();
    
            // Init capacity to length of string to encode
            var builder = new StringBuilder(toEncode.Length);
    
            foreach (char c in toEncode)
            {
                string result;
                bool success = toEscape.TryGetValue(c, out result);
    
                string character = success
                                    ? "&" + result + ";"
                                    : c.ToString();
    
                builder.Append(character);
            }
    
            return builder.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our server application runs as a service and has a ASP.NET MVC3 web-based frontend
I'm using watin testing tool for our asp.net mvc 3 web application. The same
We've been using Unity for our ASP.NET MVC3 applications. The configuration is done solely
I'm working on a rather large Asp.net MVC3 project and would like to use
I am working on a web-app with ASP.NET MVC3. We are using nHibernate with
Our ASP.NET 3.5 website running on IIS 6 has two teams that are adding
In our ASP.NET MVC 3 intranet application, configured with WindowsTokenRoleProvider for authentication, users must
I'm having trouble getting our multi-instance performance counter to work in ASP.NET MVC 3.
I am utilising Liquibase (www.liquibase.org) into our MVC3 SQL Server 2008 project to manage
I have .net mvc3 web site. And i must to implement top menu. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.