首页 > 技术文章 > ASP.NET MVC 获得 view 中的HTML并将其中的内容自动转换成繁体中文。

cjnmy36723 2016-03-17 11:40 原文

一、思路

1.获得 asp.net mvc 输出的 html 的字符串。

2.将拿到的 html 字符串中的简体中文转换成繁体中文。

3.输出 html。

二、实现

1.扩展 RazorView 视图。

    public class MainRazorView : RazorView
    {
        private readonly ILogger _logger;

        public MainRazorView(ControllerContext controllerContext, string viewPath, string layoutPath, bool runViewStartPages, IEnumerable<string> viewStartFileExtensions)
            : base(controllerContext, viewPath, layoutPath, runViewStartPages, viewStartFileExtensions)
        {
            _logger = LoggerFactory.Current.Create();
        }

        public MainRazorView(ControllerContext controllerContext, string viewPath, string layoutPath, bool runViewStartPages, IEnumerable<string> viewStartFileExtensions, IViewPageActivator viewPageActivator)
            : base(controllerContext, viewPath, layoutPath, runViewStartPages, viewStartFileExtensions, viewPageActivator)
        {
            _logger = LoggerFactory.Current.Create();
        }

        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            using (var sw = new StringWriter())
            {
                try
                {
                    //将最终生成的 html 写入到 StringWriter 中,由于这里没有使用 writer 所以当执行完这步的时候,并没有输出显示页面。
                    base.RenderView(viewContext, sw, instance);

                    //拿到了 html 的字符串。
                    var html = sw.GetStringBuilder().ToString();

                    //转换成繁体中文。
                    html = html.ToTwZh();

                    //输出显示页面,只有执行完该操作才真正显示页面。
                    writer.Write(html);
                }
                catch (Exception exception)
                {
                    _logger.Fatal("输出 HTML 失败。", exception);
                }
            }
        }
    }

ToTwZh(),这里使用了ChineseConverter库,可以直接在 Nuget 上下载,下面是扩展方法:

        /// <summary>
        /// 简体中文转成繁体中文。
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToTwZh(this string source)
        {
            return ChineseConverter.Convert(source, ChineseConversionDirection.SimplifiedToTraditional);
        }

 

2.扩展 RazorViewEngine 引擎。

public class MainRazorViewEngine : RazorViewEngine
    {
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var razorView = (RazorView)base.CreatePartialView(controllerContext, partialPath);

            if (razorView != null)
            {
                razorView = new MainRazorView(controllerContext, razorView.ViewPath, razorView.LayoutPath, razorView.RunViewStartPages, razorView.ViewStartFileExtensions);
            }

            return razorView;
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var razorView = (RazorView)base.CreateView(controllerContext, viewPath, masterPath);

            if (razorView != null)
            {
                razorView = new MainRazorView(controllerContext, razorView.ViewPath, razorView.LayoutPath, razorView.RunViewStartPages, razorView.ViewStartFileExtensions);
            }

            return razorView;
        }
    }

3.在MvcApplication中注册视图引擎。

    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            ...

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MainRazorViewEngine());
        }
    }

 

推荐阅读