Greg B
Greg B

Reputation: 14888

Remove BOM from page output via web.config

Currently our pages are being output with the Unicode BOM.

I have found one way of removing this by adding the following to my masterpage's OnInit.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Response.ContentEncoding = new System.Text.UTF8Encoding(false);
}

Where the false being passed to the UTF8Encoding constructor disables the BOM.

This works fine, but I'd prefer to set this in the web.config rather than relying on it being in the OnInit hierarchy of any given page.

The globalization element has a responseEncoding attribute which takes a string representation of a valid endcoding. e.g.

<globalization
    responseEncoding="utf-8"
    ... />

Is there any way of representing "utf-8 without BOM" as a string which could be used as the value for responseEncoding in the web.config?

Upvotes: 4

Views: 1367

Answers (1)

user3345311
user3345311

Reputation: 11

BOM or Byte Order Mark is sometimes quite annoying. Visual Studio does not change the file unless you save it (as Hans said).

And here is the solution to your problem: If you want to save a file with other encodings select save as and extend the save button in file dialog and select "Save with encoding". Or if you you want to get rid of this setting permanently just open File menu and select "Advanced save options" and there you should select "UTF-8 without signature" (and that also answered your last question :). Yes "UTF-8 without signature" is same as without BOM.

Upvotes: 1

Related Questions