[英]Serving ISO-8859-1 and UTF-8 files from ASP.NET
We have a large site with thousands of static html files. 我们有一个拥有数千个静态html文件的大型站点。 Some of them are ISO-8859-1, others are UTF-8 (with and without byte order marks).
其中一些是ISO-8859-1,另一些是UTF-8(带有和不带有字节顺序标记)。
The web.config file looks like this: web.config文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" />
</system.web>
</configuration>
If I change the fileEncoding
to "ISO-8859-1"
it works for both ISO-8859-1 and UTF-8 if there is a byte order mark . 如果我将
fileEncoding
更改为"ISO-8859-1"
则如果有字节顺序标记,它对ISO-8859-1和UTF-8 都适用 。 We are trying to avoid manually checking and adding byte order marks to files that do not have them. 我们试图避免手动检查并向不包含字节顺序标记的文件添加字节顺序标记。 Is there any way to accomplish this?
有什么办法可以做到这一点?
The files have the charset meta tag. 这些文件具有charset元标记。 Can we make the server read that to determine the file encoding?
我们可以让服务器读取它以确定文件编码吗?
EDIT 编辑
If I remove the wildcard application mapping to aspnet_isapi.dll the files are served correctly. 如果我删除了映射到aspnet_isapi.dll的通配符应用程序,则文件将正确提供。 Is there any way to have the wildcard match everything except for .html?
是否可以使通配符匹配除.html之外的所有内容?
You could automate the checking of the files to see if they contain said characters / if they are utf-8. 您可以自动检查文件,以查看它们是否包含上述字符/如果是utf-8。
This is a piece of code which pops up while googling that purpose found here : 这是在搜索此处找到的目的时弹出的一段代码:
System.Text.Encoding enc = null;
System.IO.FileStream file = new System.IO.FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read);
if (file.CanSeek)
{
byte[] bom = new byte[4]; // Get the byte-order mark, if there is one
file.Read(bom, 0, 4);
if ((bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) || // utf-8
(bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le
(bom[0] == 0xfe && bom[1] == 0xff) || // utf-16 and ucs-2
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4
{
enc = System.Text.Encoding.Unicode;
}
else
{
enc = System.Text.Encoding.ASCII;
}
// Now reposition the file cursor back to the start of the file
file.Seek(0, System.IO.SeekOrigin.Begin);
}
else
{
// The file cannot be randomly accessed, so you need to decide what to set the default to
// based on the data provided. If you're expecting data from a lot of older applications,
// default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer
// applications, default your encoding to Encoding.Unicode. Also, since binary files are
// single byte-based, so you will want to use Encoding.ASCII, even though you'll probably
// never need to use the encoding then since the Encoding classes are really meant to get
// strings from the byte array that is the file.
enc = System.Text.Encoding.ASCII;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.