zoukankan      html  css  js  c++  java
  • .NET MVC后台获得VIEW对应的html

    一、.Net Core Mvc下获得

        建立一个帮助类,如下:

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.ViewEngines;
    using Microsoft.AspNetCore.Mvc.ViewFeatures;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace NetCorePortal.Models
    {
        public class ViewHtmlHelper
        {
            public static string ConvertToString(Controller controller, string viewName, object viewModel, bool isMainPage)
            {
                controller.ViewData.Model = viewModel;
                using (StringWriter sw = new StringWriter())
                {
                    IViewEngine viewEngine = controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
                    ViewEngineResult viewEngineResult = viewEngine.FindView(controller.ControllerContext, viewName, isMainPage);
                    if (viewEngineResult.Success)
                    {
                        ViewContext viewContext = new ViewContext(
                            controller.ControllerContext,
                            viewEngineResult.View,
                            controller.ViewData,
                            controller.TempData,
                           sw,
                           new HtmlHelperOptions()
                            );
                        viewEngineResult.View.RenderAsync(viewContext);
                        return sw.GetStringBuilder().ToString();
                    }
                    else
                    {
                        return $"视图{viewName}不存在";
                    }
                }
            }
        }
    }

        后台调用方法

    public ContentResult ViewToStringDemo()
    {
        string strView = ViewHtmlHelper.ConvertToString(this, "Index", "", true);
        return Content(strView);
    }

     参考自:https://www.cjavapy.com/article/147/

    二、.Net Mvc下获得

    /// <summary>
    /// 获得LayTreeGrid视图的HTML内容
    /// </summary>
    /// <returns></returns>
    public ContentResult ViewText()
    {
         string html = string.Empty;
         IView view = ViewEngines.Engines.FindView(this.ControllerContext, "LayTreegrid", null).View;
    
         ViewDataDictionary vdd = new ViewDataDictionary();
         TempDataDictionary tdd = new TempDataDictionary();
         using (System.IO.StringWriter sw = new System.IO.StringWriter())
         {
             ViewContext viewContext = new ViewContext(this.ControllerContext, view, vdd, tdd, sw);
             viewContext.View.Render(viewContext, sw);
             html = sw.ToString();
         }
         return Content(html);
    }
  • 相关阅读:
    网站制作教程之PageAdmin网站管理系统(cms)实现多域名访问
    免费企业网站模板_学校网站模板_政府网站模板源码下载
    网站建设如何挑选自助网站建设系统
    Problem2-Project Euler
    Problem1-Project Euler
    防微杜渐——读《C安全编码标准》
    3D射影不变量
    直线匹配-LPI算法原理 Robust line matching through line–point invariants
    angular-cli 工程中使用scss文件
    JS怎么计算html标签里文字的宽度
  • 原文地址:https://www.cnblogs.com/zbspace/p/11623555.html
Copyright © 2011-2022 走看看