过年啦,回到家一直不能上网,很是一个郁闷,不过大年廿九晚上闲来无事,终于解决了MagicAjax中文乱码的问题。
自从MA 0.2.x发行以来都不能支持JS的中文,比如把JS脚本中的那个"Loading ..."换成"数据加载中 ..."后,再运行时出现的那个条就是乱码。如果更改编码格式页面又会乱掉。
哈哈,下载MA的源代码包,打开MagicAjaxModule.cs文件,看到
噢,这样呀,让它支持GB2312不就行了。
加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
编译Release文件,然后再使用这个新的DLL就OK了。
不知道,还有没有别的办法,或是以上方法都什么不当之处。
[仅就解决MA支持中文的问题]
自从MA 0.2.x发行以来都不能支持JS的中文,比如把JS脚本中的那个"Loading ..."换成"数据加载中 ..."后,再运行时出现的那个条就是乱码。如果更改编码格式页面又会乱掉。
哈哈,下载MA的源代码包,打开MagicAjaxModule.cs文件,看到
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
{
context.Response.ContentType = "text/javascript";
object cachedAjaxCallObjectJs = context.Cache["__CACHED_AJAXCALLOBJECT_JS"];
if (cachedAjaxCallObjectJs == null)
{
//read and output the embedded AjaxCallObject.js file from the manifest
using (System.IO.StreamReader reader = new System.IO.StreamReader(typeof(MagicAjaxModule).Assembly.GetManifestResourceStream("MagicAjax.script.AjaxCallObject.js")))
{
cachedAjaxCallObjectJs = reader.ReadToEnd();
}
context.Cache.Insert("__CACHED_AJAXCALLOBJECT_JS", cachedAjaxCallObjectJs);
}
context.Response.Write(cachedAjaxCallObjectJs);
context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
context.Response.End();
}
}
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
{
context.Response.ContentType = "text/javascript";
object cachedAjaxCallObjectJs = context.Cache["__CACHED_AJAXCALLOBJECT_JS"];
if (cachedAjaxCallObjectJs == null)
{
//read and output the embedded AjaxCallObject.js file from the manifest
using (System.IO.StreamReader reader = new System.IO.StreamReader(typeof(MagicAjaxModule).Assembly.GetManifestResourceStream("MagicAjax.script.AjaxCallObject.js")))
{
cachedAjaxCallObjectJs = reader.ReadToEnd();
}
context.Cache.Insert("__CACHED_AJAXCALLOBJECT_JS", cachedAjaxCallObjectJs);
}
context.Response.Write(cachedAjaxCallObjectJs);
context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
context.Response.End();
}
}
噢,这样呀,让它支持GB2312不就行了。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
...
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
...
加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
编译Release文件,然后再使用这个新的DLL就OK了。
不知道,还有没有别的办法,或是以上方法都什么不当之处。
[仅就解决MA支持中文的问题]