简体   繁体   English

从ASP.NET提供ISO-8859-1和UTF-8文件

[英]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.

相关问题 C# 将字符串从 UTF-8 转换为 ISO-8859-1 (Latin1) H - C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H 使用带有ISO-8859-1编码的XmlTextWriter编写XML文件 - Writing XML files using XmlTextWriter with ISO-8859-1 encoding 从UTF-8转换为ISO-8859-15时会自动替换哪些双引号字符? - Which double quote characters are automatically replaced when converting from UTF-8 to ISO-8859-15? 从 WCF 调用使用 ISO-8859-1 编码的网络服务 - Calling a webservice that uses ISO-8859-1 encoding from WCF VB Streamwriter设置为ISO-8859-1,但文件获取UTF8-无Bom编码 - VB Streamwriter set to ISO-8859-1 but file get UTF8-Without Bom encoding 在ASP.NET MVC中的参数中编码非UTF-8文本 - Encoding non UTF-8 text in Parameters in ASP.NET MVC C#HtmlEncode-ISO-8859-1实体名称与编号 - C# HtmlEncode - ISO-8859-1 Entity Names vs Numbers System.Text.Encoding.GetEncoding(“iso-8859-1”)抛出PlatformNotSupportedException? - System.Text.Encoding.GetEncoding(“iso-8859-1”) throws PlatformNotSupportedException? 将字符串从ISO转换为以空字节结尾的UTF-8 - Convert a string from ISO to UTF-8 with null byte ending Web APi Vs MVC和ASP.net核心Web APi中的静态文件 - Web APi Vs MVC and Serving static files in ASP.net core Web APi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM