1.首先引用cookie Jquery
<script type="text/javascript"src="~/script/jquery.cookie.js"></script>
2.JS方法:
function TraditionalChina(res) {
$.cookie('IsSimple', res, { expires: 1, path: '/' });
location.reload();
}
/*
调用方法:
<a href="javascript:;"onclick="TraditionalChina(false)">简体</a>
<a href="javascript:;"onclick="TraditionalChina(true)">繁體</a>
*/
3.后台切换代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using TOOL;
using DAL;
namespace Controllers
{
public class CompressController : Controller
{
public class ViewCompressAttribute : ActionFilterAttribute
{
/// <summary>
/// 压缩html代码
/// </summary>
/// <param name="text">html代码</param>
/// <returns></returns>
private static string Compress(string text)
{
var reg = new Regex(@"\s*(</?[^\s/>]+[^>]*>)\s+(</?[^\s/>]+[^>]*>)\s*");
text = reg.Replace(text, m => m.Groups[1].Value + m.Groups[2].Value);
//移除html标签之间的空格
text = new Regex(@"(?<=>)[\s|\n|\t]*(?=<)").Replace(text, string.Empty);
//移除多余空格和换行符
text = new Regex(@"\n+\s+").Replace(text, string.Empty);
return text;
}
/// <summary>
/// 在执行Action的时候,就把需要的Writer存起来
/// </summary>
/// <param name="filterContext">上下文</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_builder = new StringBuilder();
_writer = new StringWriter(_builder);
_htmlWriter = new HtmlTextWriter(_writer);
_output = (HttpWriter)filterContext.RequestContext.HttpContext.Response.Output;
filterContext.RequestContext.HttpContext.Response.Output = _htmlWriter;
base.OnActionExecuting(filterContext);
}
/// <summary>
/// 在执行完成后,处理得到的HTML,并将他输出到前台
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var response = Compress(_builder.ToString());
//简繁体切换
try
{
HttpCookie cookie = filterContext.HttpContext.Request.Cookies["IsSimple"];
if (cookie != null)
{
var res =Convert.ToBoolean(cookie.Value);
if (res)
response = ChineseChange.ToTraditional(response);
}
}
catch {
}
_output.Write(response);
}
#region Private
/// <summary>
/// HtmlTextWriter
/// </summary>
private HtmlTextWriter _htmlWriter;
/// <summary>
/// StringWriter
/// </summary>
private StringWriter _writer;
/// <summary>
/// StringBuilder
/// </summary>
private StringBuilder _builder;
/// <summary>
/// HttpWriter
/// </summary>
private HttpWriter _output;
#endregion
}
}
}
public class ChineseChange
{
public static class SystemChineseConverter
{
internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
/// <summary>
/// 使用OS的kernel.dll做為簡繁轉換工具,只要有裝OS就可以使用,不用額外引用dll,但只能做逐字轉換,無法進行詞意的轉換
/// <para>所以無法將電腦轉成計算機</para>
/// </summary>
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);
/// <summary>
/// 繁體轉簡體
/// </summary>
/// <param name="pSource">要轉換的繁體字:體</param>
/// <returns>轉換後的簡體字:体</returns>
public static string ToSimplified(string pSource)
{
String tTarget = new String(' ', pSource.Length);
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
return tTarget;
}
/// <summary>
/// 簡體轉繁體
/// </summary>
/// <param name="pSource">要轉換的繁體字:体</param>
/// <returns>轉換後的簡體字:體</returns>
public static string ToTraditional(string pSource)
{
String tTarget = new String(' ', pSource.Length);
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
return tTarget;
}
}
}