web.config中的<globalization >标签在将几个不同服务器上的网闸内容以统一面貌集成在自己网站上时的要考虑的问题

本文详细介绍了在网站开发过程中遇到的文件名乱码问题,通过分析web.config中的globalization标签配置,发现其导致上传文件名乱码的原因,并提供了解决方案。通过修改web.config配置和调整母板页内的编码设置,成功解决了文件名乱码问题,同时在集成其他服务器内容时,根据目标网站编码需求添加特定的web.config文件,确保信息正确返回和解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天,忽然发现web.config中的<globalization >标签的重要,尤其是在将几个不同服务器上的网闸内容以统一面貌集成在自己网站上时,容易出错,例如下面:

-------------------------------------------------------------------------------------------------------------

在http://blog.youkuaiyun.com/goodshot/article/details/8166410提到下面内容:

 4.遇到问题:

在正常的一张网页上传中,使用上面的代码不会产生任何文件名乱码的情况。

但是,当用在有母板页的页面上传部位,我发现总是出现文件名乱码错误。

查了一下:母板(包括运行后查看源码)内的编码charset都是utf-8,但是该网页在浏览时却采用了gb2312的方式,可能是是这个造成的上传文件名乱码(不过这个在不是母板生成的单独的上传网页测试,100%上传无乱码)

于是,在ashx文件中添加了代码转换的部分:

    public void ProcessRequest(HttpContext  context)  
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";
       
      
        HttpPostedFile oFile = context.Request.Files["Filedata"];


        byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(oFile.FileName);
        buffer = Encoding.Convert(Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("gb2312"), buffer);
        string fn = Encoding.UTF8.GetString(buffer);//网页编码 http://blog.youkuaiyun.com/goodshot/article/details/8627731



        string strUploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\" + HttpContext.Current.User.Identity.Name + "\\";


        if (oFile != null)
        {


            if (!Directory.Exists(strUploadPath))
            {
                Directory.CreateDirectory(strUploadPath);
            }
            oFile.SaveAs(strUploadPath + oFile.FileName);




            //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
            context.Response.Write("1");
        }
        else
        {
            context.Response.Write("0");
        }
 
    }

 运行后虽能解决部分问题,但仍有部分文件名出现乱码。


最终解决问题发现:问题实在网站的web.config中有这样的设置:

<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>

去掉后(默认发送和接受都是utf-8),去掉上面添加的红色编码转换部分。成功!


--------------------------------------------------------------------------------------------------------------

但是,测试另外一个利用其他服务器内容能够,将其集成到我的系统的其他网站内容时的模块时(模拟http申请网站信息),原来能正常返回并解析的内容突然找不到了。

稍一分析,很有可能是去掉了上面的<globalization>,造成了原来能够申请到信息的网站无法根据关键字返回信息,对方很可能就是符合发送申请的GB2312标准。于是,在该子模块所在的文件夹下添加一个web.config,单独添加<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>内容,覆盖根目录下刚才默认的UTF-8标准。测试,成功!

方法,简单易行,只要明白原理,一切都轻松!


<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="DB-NetShopsConnectionString" connectionString="Data Source=.\MSSQLSERVER2012;Initial Catalog=DB-NetShops;User ID=sa;Password=your_password;Integrated Security=False" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <!-- 添加全局cookie设置 --> <httpCookies httpOnlyCookies="true" /> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" maxRequestLength="20480" executionTimeout="300" requestValidationMode="4.7.2" /> <!-- 移除httpOnlyCookies属性 --> <sessionState mode="InProc" timeout="20" /> <authentication mode="Forms"> <!-- 移除httpOnlyCookies属性 --> <forms loginUrl="~/WebForm1.aspx" defaultUrl="~/WebForm2.aspx" timeout="20" /> </authentication> <globalization culture="zh-CN" uiCulture="zh-CN" requestEncoding="utf-8" responseEncoding="utf-8" /> <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"> <error statusCode="404" redirect="~/404.aspx" /> <error statusCode="500" redirect="~/500.aspx" /> </customErrors> <pages masterPageFile="~/Site.master"> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> </pages> </system.web> <system.webServer> <httpProtocol> <customHeaders> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="X-Xss-Protection" value="1; mode=block" /> </customHeaders> </httpProtocol> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> <security> <requestFiltering> <requestLimits maxAllowedContentLength="20971520" /> </requestFiltering> </security> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> </compilers> </system.codedom> </configuration> 添加母版页代码设置,生成完整webconfig代码
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值