最近在研究ASP.NET MVC生成静态页,那么今天也算个学习笔记吧!
实现原理及步骤:
1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView;
2.设置上下文对象里的Model;
3.调用视图的Render()方法,将渲染结果保存到物理静态文件;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
using System;
using System.IO;
using System.Text;
using System.Web.Mvc;
namespace Whir.Foundation.UI
{
/// <summary>
/// 描述:静态页面生成帮助类
/// </summary>
public class StaticPageHelper
{
/// <summary>
/// 根据View视图生成静态页面
/// </summary>
/// <param name="htmlPath">存放静态页面所在绝对路径</param>
/// <param name="context">ControllerContext</param>
/// <param name="viewPath">视图名称</param>
/// <param name="masterName">模板视图名称</param>
/// <param name="model">参数实体模型</param>
/// <param name="html">返回信息</param>
/// <param name="isPartial">是否分布视图</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 ;
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);
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;
}
}
}
|
AjaxResult 是自己封装的一个类,您也可以用自己封装的类代替。
?
1
2
3
4
5
|
public class AjaxResult
{
public bool IsSucess { get ; set ; }
public string Body { get ; set ; }
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/a497785609/article/details/51354851
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 71
-
2025-05-29 87
-
2025-05-25 42
-
2025-05-25 87
-
2025-05-25 81
热门评论