您的位置:首页 > 编程语言 > ASP

ASP.NET MVC的Razor引擎:RazorViewEngine

2012-09-07 09:10 465 查看
[code] public class SimpleRazorViewEngine: IViewEngine

{

 private string[] viewLocationFormats = new string[]{ 

"~/Views/{1}/{0}.cshtml",

"~/Views/{1}/{0}.vbhtml",

"~/Views/Shared/{0}.cshtml",

"~/Views/Shared/{0}.vbhtml"};

 private string[] areaViewLocationFormats = new string[]{ 

"~/Areas/{2}/Views/{1}/{0}.cshtml",

"~/Areas/{2}/Views/{1}/{0}.vbhtml",

"~/Areas/{2}/Views/Shared/{0}.cshtml",

"~/Areas/{2}/Views/Shared/{0}.vbhtml"};


 public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)

{

 return FindView(controllerContext, partialViewName, null, useCache);

}


 public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)

{

 string controllerName = controllerContext.RouteData.GetRequiredString("controller");

 object areaName;

 List<string> viewLocations = new List<string>();

 Array.ForEach(viewLocationFormats, format => viewLocations.Add(string.Format(format, viewName, controllerName)));

 if (controllerContext.RouteData.Values.TryGetValue("area", out areaName))

{

 Array.ForEach(areaViewLocationFormats, format=>viewLocations.Add(string.Format(format,viewName,controllerName, areaName)));

}

 foreach (string viewLocation in viewLocations)

{

 string filePath = controllerContext.HttpContext.Request.MapPath(viewLocation);

 if (File.Exists(filePath))

{

 return new ViewEngineResult(new SimpleRazorView(viewLocation), this);

}

}

 return new ViewEngineResult(viewLocations);

}


 public void ReleaseView(ControllerContext controllerContext, IView view)

{

 IDisposable disposable = view as IDisposable;

 if (null != disposable)

{

 disposable.Dispose();

}

}

}

[/code]
[/code]
我们完全按照上面介绍的路径顺序搜索指定的目标View。简单起见,我们在对目标View进行搜索时忽略了指定的布局文件名和对ViewEngineResult的缓存。这个自定义的SimpleRazorViewEngine在Global.asax中通过如下的代码对进行注册。

[code]
[code] public class MvcApplication : System.Web.HttpApplication

{

 protected void Application_Start()

{

 //其他操作

 ViewEngines.Engines.Clear();

 ViewEngines.Engines.Add(new SimpleRazorViewEngine());

}

}

[/code]
[/code]
我们定义了如下一个HomeController:

[code]
[code] public class HomeController : Controller

{

 public ActionResult Index()

{

 Contact contact = new Contact 

{ 

Name = "张三", 

PhoneNo= "123456789", 

EmailAddress = "zhangsan@gmail.com" 

};

 return View(contact);

}

}


public class Contact

{

 [DisplayName("姓名")]

 public string Name{ get; set;}


 [DisplayName("电话号码")]

 public string PhoneNo{ get; set;}


 [DisplayName("电子邮箱地址")]

 public string EmailAddress{ get; set;}

}

[/code]
[/code]
我们的View很简单。如下面的代码片断所示,这是一个Model类型为Contact的强类型View,在该View中我们直接调用HtmlHelper<TModel>的扩展方法EditorForModel将作为Model的Contact对象以编辑模式呈现在一个表单之中。

[code]
[code] @model Contact

@{

 ViewBag.Title = Model.Name;

}


@using (Html.BeginForm())

{ 

 @Html.EditorForModel()

 <input type="submit" value="保存" />

}

[/code]
[/code]
为了验证我们自定义的SimpleRazorView对布局文件和_ViewStart页面的支持,我们在“~/Views/Shared/”目录下定义了如下一个名为“_Layout.cshtml”的布局文件。布局文件的设置通过定义在“~/Views/”目录下具有如下定义的“_ViewStart.cshtml”文件来指定。

[code]
[code] _Layout.cshtml:

<html>

 <head>

 <title>@ViewBag.Title </title>

 </head>

 <body>

 <h3>编辑联系人信息</h3>

 @RenderBody()

 </body>

</html>


_ViewStart.cshtml:

@{

 Layout = "~/Views/Shared/_Layout.cshtml";

}

[/code]
[/code]
运行我们的程序后会在浏览器中呈现如下图所示的输出结果,可以看出这和我们直接在Action方法Index中返回一个ViewResult对象没有什么不同。





ASP.NET MVC的Razor引擎:View编译原理
ASP.NET MVC的Razor引擎:RazorView
ASP.NET MVC的Razor引擎:IoC在View激活过程中的应用
ASP.NET MVC的Razor引擎:RazorViewEngine
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: