首页 存档 技术 查看内容

自己动手写一个简单的MVC框架(下)

2018-3-30 13:00 |来自: 互联网 348 0

摘要: 接上文 (3)View 在ASP.NET MVC中提供了aspx与Razor等模板引擎,这里我偷了懒,直接借助了NVelocity模板引擎来实现。因此,这个文件夹中只有一个VelocityHelper类(我直接从网上搜索的),该类可以帮助我们找到指 ...

接上文


(3)View


在ASP.NET MVC中提供了aspx与Razor等模板引擎,这里我偷了懒,直接借助了NVelocity模板引擎来实现。因此,这个文件夹中只有一个VelocityHelper类(我直接从网上搜索的),该类可以帮助我们找到指定的HTML并绑定Model实体。


///

/// NVelocity模板工具类 VelocityHelper

///

publicclassVelocityHelper

{

privateVelocityEngine velocity =null;

privateIContext context =null;

publicobjectYZControl {get;privateset; }

///

/// 构造函数

///

/// 模板文件夹路径

publicVelocityHelper(stringtemplatDir)

{

Init(templatDir);

}

///

/// 无参数构造函数

///

publicVelocityHelper() { }

///

/// 初始话NVelocity模块

///

publicvoidInit(stringtemplatDir)

{

// 创建VelocityEngine实例对象

velocity =newVelocityEngine();

// 使用设置初始化VelocityEngine

ExtendedProperties props =newExtendedProperties();

props.AddProperty(RuntimeConstants.RESOURCE_LOADER,"file");

props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templatDir));

//props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(HttpContext.Current.Request.PhysicalPath));

props.AddProperty(RuntimeConstants.INPUT_ENCODING,"utf-8");

props.AddProperty(RuntimeConstants.OUTPUT_ENCODING,"utf-8");

// 模板的缓存设置

props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,true);//是否缓存

props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30);//缓存时间(秒)

velocity.Init(props);

// 为模板变量赋值

context =newVelocityContext();

}

///

/// 给模板变量赋值

///

/// 模板变量

/// 模板变量值

publicvoidPut(stringkey,objectvalue)

{

if(context ==null)

{

context =newVelocityContext();

}

context.Put(key, value);

}

///

/// 显示模板

///

/// 模板文件名

publicvoidDisplay(stringtemplatFileName)

{

// 从文件中读取模板

Template template = velocity.GetTemplate(templatFileName);

// 合并模板

StringWriter writer =newStringWriter();

template.Merge(context, writer);

// 输出

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Write(writer.ToString());

HttpContext.Current.Response.Flush();

HttpContext.Current.Response.End();

}

///

/// 根据模板生成静态页面

///

///

///

publicvoidCreateHtml(stringtemplatFileName,stringhtmlpath)

{

// 从文件中读取模板

Template template = velocity.GetTemplate(templatFileName);

// 合并模板

StringWriter writer =newStringWriter();

template.Merge(context, writer);

using(StreamWriter write2 =newStreamWriter(HttpContext.Current.Server.MapPath(htmlpath),false, Encoding.UTF8, 200))

{

write2.Write(writer);

write2.Flush();

write2.Close();

}

}

///

/// 根据模板生成静态页面

///

///

///

//public void CreateJS(string templatFileName, string htmlpath)

//{

// //从文件中读取模板

// Template template = velocity.GetTemplate(templatFileName);

// //合并模板

// StringWriter writer = new StringWriter();

// template.Merge(context, writer);

// using (StreamWriter write2 = new StreamWriter(HttpContext.Current.Server.MapPath(htmlpath), false, Encoding.UTF8, 200))

// {

// write2.Write(YZControl.Strings.Html2Js(YZControl.Strings.ZipHtml(writer.ToString())));

// write2.Flush();

// write2.Close();

// }

//}

}


三、我的MVC框架应用实例


3.1 MVC 应用DEMO介绍



这是一个ASP.NET 空Web应用项目搭建起来的MVC Web应用项目,它移除了自带的所有引用项目,仅仅保留了System和System.Web,做到了尽可能地“纯净”。通过引入Mvc.Lib核心类库,建立Controller、Model和View文件夹以及对应的类和HTML来实现MVC模式。


(1)引入Mvc.Lib核心类库之后,需要配置一下Web.config,使UrlRoutingModule能够正常工作:


声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

相关分类

返回顶部