Strange characters output on asp.net mvc page

2019-07-22 17:22发布

问题:

Sometimes (randomly) my asp.net mvc application outputs page where single character is being replaced with weird question marks, as shown on image

This behavior is constant until data changes, and it does not disappear with page refresh. The whole page is in Georgian unicode. I am using asp.net mvc 3 with visual studio 2010 and iis express. This is also observed on published site to IIS 7.5. I have seen similar issues in asp.net webforms application too.

I have observed this behavior not only on db-provided data, but simple anchors, generated with html helper

TagBuilder addAnchor = new TagBuilder("a");
addAnchor.AddCssClass("add-item");
addAnchor.MergeAttribute("href", "#");
addAnchor.SetInnerText(SharedResources.Add); //resource item contains string დამატება

which output anchor "დამატ���ბა"

And this is not issue with page rendering on browser-side, as raw response already contains these symbols.

Update:

As it seems, disabling buffer output by setting Response.BufferOutput = false; removes weird characters from page. But I use Cassette for referencing bundles, and it cannot work with disabled buffer output - no scipts or stylesheets get referenced on page.

Has anyone of you had a similar problem? This is actually critical for me, as currently it affects data where that single character being damaged is the correct answer information of a multi-select test question.

回答1:

I had the same bug. It is Cassette with a feature that rewrites the result HTML content (and needs BufferOutput set to true) to move referenced script and stylesheet tags to the top if needed. See the IsHtmlRewritingEnabled option at http://getcassette.net/documentation/configuration. When I turned off this feature, the bug has disappeared.

My observations:

  1. It is appeared only with remote requests. Local requests are always ok.
  2. The location of question marks is always constant for all clients, browsers and computers.
  3. The location of question marks changes only after recompilation of the application.


回答2:

I would imagine it's to do with character sets. I've had this issue when i've tried to display a character that isn't in a particular character set.

such as ’ or – (’ or –) in UTF-8 when the character hasn't been HTML encoded previously.



回答3:

I too had output like this. In my case the source of error was within Views/web.config. An assembly was listed in pages/namespaces, which was not part of the referenced assemblies