以前做CMS的时候都会根据模板来生成输出HTML或者生成HTML文件。

常用的引擎有VTemplate、NVelocity等等,这个我就布做介绍了。

这里我想说的是。当mvc出现Razor模板引擎的时候。我就在想怎么利用这个MVC自带的模板生产HTML文件。

想利用Razor模板引擎来生成文件,首先你该了解MVC的生命周期。这个在此我也不做多做介绍了。

入主题吧:

1.我的想法比较简单,在一个control中进行模板生产。第一步就是找到你设定的模板。这个比较简单,用mvc提供的方法,找到这个视图。如果路径和控制器的规则一样的话你就写个模板文件的名字,否则全路径吧。

 IView v = ViewEngines.Engines.FindView(cc, tempUrl, "").View;

2.传入请求信息和初始化模板中页面的变量值。

当找到模板后我们是不是要把模板中和我们设定的信息结合在一起生成我们想要的html呢?其实这个我们也不用考虑很多,既然用MVC的Razor模板引擎,那我们就考虑使用ViewContext

public static string OutHtml(ControllerContext cc, string tempUrl, ViewDataDictionary vd, TempDataDictionary td)
{
string html = string.Empty;
IView v = ViewEngines.Engines.FindView(cc, tempUrl, "").View;
using (StringWriter sw = new StringWriter())
{
ViewContext vc = new ViewContext(cc, v, vd, td, sw);
vc.View.Render(vc, sw);
html = sw.ToString();
}
return html;
}

3.到这一步我们基本上就能获取到加载模板后生产的html字符了。

在control中我们可以这些写。

    public ActionResult CreateHtml()
{
ViewBag.Title = "输出的HTML页面";//这个变量在模板页中有声明的话就会传入模板页最终替换模板页面的变量(和mvc的razor一样用)
ViewBag.my = "动态生成,。。。。。";
string outHtml= RazorHtml.OutHtml(this.ControllerContext, "Title", this.ViewData, this.TempData);
ViewBag.HtmlContent = outHtml;
return View();
}

在模板中写上我们定义好的参数

<html>

<head><title>@ViewBag.Title</title></head>
<body>
<h2>@ViewBag.my<h2>
</body>
</html>

运行一下看一下结果

到了这一步生成也没什么问题了吧。

4.接下来我们看页面文件生成吧。这方面没什么好说的直接贴上代码,(很久以前的了文件生成代码了。)

 public static string OutHtml(ControllerContext cc, string tempUrl, ViewDataDictionary vd, TempDataDictionary td)
{
string html = string.Empty;
IView v = ViewEngines.Engines.FindView(cc, tempUrl, "").View;
using (StringWriter sw = new StringWriter())
{
ViewContext vc = new ViewContext(cc, v, vd, td, sw);
vc.View.Render(vc, sw);
html = sw.ToString();
}
return html;
} public static void SaveHtml(ControllerContext cc, string tempUrl, ViewDataDictionary vd, TempDataDictionary td, string savePath, string fileName, string Extension, Encoding encoding)
{
string html = string.Empty;
IView v = ViewEngines.Engines.FindView(cc, tempUrl, "").View;
using (StringWriter sw = new StringWriter())
{
ViewContext vc = new ViewContext(cc, v, vd, td, sw);
vc.View.Render(vc, sw);
html = sw.ToString();
}
CreateSaveFile(savePath, encoding, html);
} /// <summary>
/// 写入文件
/// </summary>
/// <param name="filePath">保存地址</param>
/// <param name="enconding">编码类型</param>
/// <param name="content">内容</param>
/// <returns></returns>
public static bool SaveFile(string filePath, Encoding enconding, string content)
{
try
{
File.SetAttributes(System.Web.HttpContext.Current.Server.MapPath(filePath), FileAttributes.Normal);
using (FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath(filePath), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//fs.a = fs.Attributes & ~FileAttributes.ReadOnly & ~FileAttributes.Hidden;
Byte[] info = enconding.GetBytes(content);
fs.Write(info, , info.Length);
fs.Close();
return true;
}
}
catch (Exception)
{ return false;
} } /// <summary>
/// 创建文件
/// </summary>
/// <param name="filePath">保存地址</param>
/// <param name="enconding">编码类型</param>
/// <param name="content">内容</param>
/// <returns></returns>
public static bool CreateSaveFile(string filePath, Encoding enconding, string content)
{
try
{
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(filePath)))//如果不存在就创建file文件夹
{
FileStream fs;
fs = File.Create(System.Web.HttpContext.Current.Server.MapPath(filePath));
fs.Close();
fs.Dispose();
return true;
} SaveFile(filePath, enconding, content);
return true; }
catch (Exception)
{
return false;
} }
}

控制器中写上生成代码

  ViewBag.Title = "输出的HTML页面";//这个变量在模板页中有声明的话就会传入模板页最终替换模板页面的变量(和mvc的razor一样用)
ViewBag.my = "动态生成,。。。。。";
string outHtml= RazorHtml.OutHtml(this.ControllerContext, "Title", this.ViewData, this.TempData);
ViewBag.HtmlContent = outHtml;
RazorHtml.SaveHtml(this.ControllerContext, "Title", this.ViewData, this.TempData, "/StaticHtml/Loui.html", "Loui.html", "html", Encoding.UTF8);//生成的HTML
return View();

看一下结果,刚刚的

总结:只是一个突然的想法,也没做过其他的测试,不过我知道每次修改模板的mvc也会动态更新生成模板的cs文件。就是说你修改过一次模板然后页面的首次加载会很慢。以后就不会了。

MVC Razor模板引擎输出HTML或者生产HTML文件的更多相关文章

  1. [转]MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    本文参考自下面文章整理 MVC Razor模板引擎 @RenderBody.@RenderPage.@RenderSection及Html.RenderPartial.Html.RenderActio ...

  2. Asp.net MVC Razor模板引擎技巧分享

    Razor是Asp.net MVC中新的默认模板类型, 语法简单易用.这篇文章不涉及Razor的语法,主要介绍Razor的一些在MVC项目中的使用技巧,以及脱离MVC环境下,如何使用Razor. 阅读 ...

  3. MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  4. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页

    https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...

  5. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  6. 脱离MVC使用Razor模板引擎

    关于Razor模板引擎 1.简介 模板引擎:Razor.Nveocity.Vtemplate.Razor有VS自动提示.使用起来会方便一点. 但是Razor大多是在MVC下使用的. 那么如何在非MVC ...

  7. MVC小系列(二)【Razor 模板引擎】

    Razor 模板引擎 Razor模板页:它使我们不用再使用master模板了 一 :@Url.Content:是可以加载CSS和JS等文件比如: <link href="@Url.Co ...

  8. Razor模板引擎 (RazorEngine)

    Razor模板引擎不仅在ASP.NET MVC中内置了Razor模板引擎,还有一个开源的RazorEngine, 这样以来我们可以在非ASP.NET MVC项目中使用Razor引擎,甚至在控制台,Wi ...

  9. Razor - 模板引擎 / 代码生成 - RazorEngine

    目录 Brief Authors Official Website RazorEngine 的原理 - 官方解释 安装记录 Supported Syntax (默认实现支持的语法) 测试记录 - ca ...

随机推荐

  1. iface eth0 inet dhcp

  2. 【MySQL】MySQL索引背后的之使用策略及优化【转】

    转自:http://database.ctocio.com.cn/353/11664853.shtml 另外很不错的对于索引及索引优化的文章: http://www.cnblogs.com/magia ...

  3. ios开发之OC基础-类和对象

    本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...

  4. Oracle笔记 十四、查询XML操作、操作系统文件

    --1.随机数 select dbms_random.value from dual; select mod(dbms_random.random, 10) from dual; --0-9随机数 s ...

  5. 银行ATM机工作流程模拟编程(代码)

    #include<stdio.h>#include<stdlib.h>#include <conio.h>#include <string.h> voi ...

  6. 如何使用CSS3画出一个叮当猫

    刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易 的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接 ...

  7. SqlServer将表中数据复制到另一张表

    insert into phone2(ph,attr,type,carrier) select top 1000 ph,attr,type,carrier from phone 将表phone的字段和 ...

  8. 设置trace SQL

    Select PeopleTools, Utilities, Debug, Trace SQL to access the Trace SQL page. You use this page to c ...

  9. luigi学习8--使用中央调度器

    --local-scheduler一般用在开发阶段,这在一个产品中是不建议这样使用的.使用中央调度器有两个目的: 保证两个相同的task不会同时运行两次 提供一个可视化的界面 注意:中央调度器并不会帮 ...

  10. Lua 练习中的Bug 以及日志

    使用 Lua 中的table.getn获得数组的table的长度:运行失败-- > t ={1,2,3 } > print(table.getn(t)) stdin:1: attempt ...