• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMVC创建静态页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.在MVC下新建一个类:StaticPageHelper

public class StaticPageHelper
{
    /// <summary>
    /// 根据View视图生成静态页面  
    /// </summary>
    /// <param name="viewPath">视图名称</param>
    /// <param name="htmlPath">存放静态页面所在绝对路径</param>
    /// <param name="context">控制器上下文</param>
    /// <param name="model">参数实体模型</param>
    /// <param name="isPartial">是否分布视图</param>
    /// <param name="masterName">模板视图名称</param>
    /// <returns>生成成功返回true,失败false</returns>
    public static AjaxResult GenerateStaticPage(string viewPath, string htmlPath, ControllerContext context, object model = null, bool isPartial = false, string masterName = "")
    {
        var ajaxResult = new AjaxResult();
        try
        {
            //创建存放静态页面目录                              
            if (!Directory.Exists(Path.GetDirectoryName(htmlPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(htmlPath));
            }
            //删除已有的静态页面  
            if (File.Exists(htmlPath))
            {
                File.Delete(htmlPath);
            }
            ViewEngineResult result = null;
            //通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView
            if (isPartial)
            {
                result = ViewEngines.Engines.FindPartialView(context, viewPath);
            }
            else
            {
                result = ViewEngines.Engines.FindView(context, viewPath, masterName);
            }

            if (model != null)
            {
                context.Controller.ViewData.Model = model;
            }

            /* 
                * 设置临时数据字典作为静态化标识 
                * 可以在视图上使用TempData["IsStatic"]来控制某些元素显示。 
                */
            if (!context.Controller.TempData.ContainsKey("IsStatic"))
            {
                context.Controller.TempData.Add("IsStatic", true);
            }

            if (result.View != null)
            {
                using (var sw = new StringWriter())
                {
                    var viewContext = new ViewContext(context, result.View, context.Controller.ViewData, context.Controller.TempData, sw);
                    //调用视图的Render()方法,将渲染结果保存到物理静态文件
                    result.View.Render(viewContext, sw);

                    string body = sw.ToString();
                    File.WriteAllText(htmlPath, body, Encoding.UTF8);
                    ajaxResult.IsSucess = true;
                    ajaxResult.Body = "存放路径:" + htmlPath;
                }
            }
            else
            {
                ajaxResult.IsSucess = false;
                ajaxResult.Body = "生成静态页面失败!未找到视图!";
            }
        }
        catch (IOException ex)
        {
            ajaxResult.IsSucess = false;
            ajaxResult.Body = ex.Message;
        }
        catch (Exception ex)
        {
            ajaxResult.IsSucess = false;
            ajaxResult.Body = ex.Message;
        }
        return ajaxResult;
    }
}

public class AjaxResult
{
    public bool IsSucess { get; set; }
    public string Body { get; set; }
}

2.创建广告静态页与调用方法:

/// <summary>
/// 创建广告静态页
/// </summary>
/// <param name="LX_Column"></param>
/// <returns></returns>
private AjaxResult CreateStaticPage(string LX_Column)
{
    AjaxResult ajaxResult = new AjaxResult();
    View_Home view_Home = new View_Home();
    view_Home.CommandAdList = GetCommandAdList(LX_Column);
    string commandPage = ""; //模板页
    string commandStaticPage = ""; //生成的静态面
    if (view_Home.CommandAdList != null)
    {
        if (view_Home.CommandAdList.Count > 0)
        {
            if (view_Home.CommandAdList.FirstOrDefault().LX_Column == (int)Models.CommandAdLX_ColumnType.Index)
            {
                commandPage = "/Views/HtmlTemplate/CommandAd/CommandAdList.cshtml";
                commandStaticPage = "/Static/Mobile/Home/CommandAdList.html";
            }
            else
            {
                commandPage = "/Views/HtmlTemplate/CommandAd/CommandAdThemeList.cshtml";
                commandStaticPage = "/Static/Mobile/Home/CommandAdThemeList.html";
            }
            ajaxResult = StaticPageHelper.GenerateStaticPage(commandPage, Server.MapPath(commandStaticPage), ControllerContext, view_Home, true);
        }
    }
    return ajaxResult;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap