asp.net MVC implements one of its own view engines

Source: Internet
Author: User
Tags httpcontext

In ASP.net mvc, an open source project Mvccontrib, we provide several view engines, such as Nvelocity, Brail, Nhaml, and XSLT. So what should we do if we want to implement one of our own view engines in asp.net mvc?

We know that rendering views are implemented in controller by passing the view name and data to the Renderview () method. Okay, we'll do it from here. Let's look at the source code for ASP.net mvc and see how the Renderview () method is implemented:

protected virtual void RenderView(string viewName, string
masterName, object viewData) {
ViewContext viewContext = new ViewContext(
ControllerContext, viewName, masterName, viewData, TempData);
ViewEngine.RenderView(viewContext);
}//

This is the source of P2, P3 slightly different, the principle is similar, from the above code we can see that the controller in the Renderview () method is mainly ControllerContext, ViewName, Mastername, ViewData , tempdata this pile of stuff into viewcontext, and then passes ViewContext to Viewengine.renderview (ViewContext). Well, yes, what we're going to achieve here is the Viewengine Renderview () method.

asp.net mvc provides us with a default view engine, the view engine called: Webformsviewengine. As you can see from the name, the view engine is rendered using ASP.net Web forms. Here, we want to implement the view engine using the template with HTML file bar, simple template sample code as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html XMLns=""http://www.w3.org/1999/xhtml"">
http://www.w3.org/1999/xhtml" >
<head>
  <title>自定义视图引擎示例</title>
</head>
<body>
  <h1>{$ViewData.Title}</h1>
  <p>{$ViewData.Message}</p>
  <p>The following fruit is part of a string array: {$ViewData.FruitStrings[1]}</p>
  <p>The following fruit is part of an object array: {$ViewData.FruitObjects[1].Name}</p>
  <p>Here's an undefined variable: {$UNDEFINED}</p>
</body>
< ml>

Let's start our implementation immediately below. First of all, there is no doubt that we are going to create a viewengine, just named Simpleviewengine Bar, note Oh, viewengine to implement Iviewengine interface:

public class Simpleviewengine:iviewengine
{
#region Private Members
Iviewlocator _viewlocator = null;
#endregion
#region Iviewengine Members:renderview ()
public void Renderview (ViewContext viewcontext)
{
String viewlocation = Viewlocator.getviewlocation
(ViewContext, Viewcontext.viewname);
if (string. IsNullOrEmpty (viewlocation))
{
throw new InvalidOperationException (string. Format
("View {0} could not being found.", Viewcontext.viewname));
}
String viewPath = ViewContext.HttpContext.Request.MapPath (viewlocation);
String viewtemplate = File.readalltext (ViewPath);
The following is a template parsing
Irenderer renderer = new Printrenderer ();
Viewtemplate = renderer. Render (Viewtemplate, ViewContext);
ViewContext.HttpContext.Response.Write (viewtemplate);
}
#endregion
#region Public Properties:viewlocator
Public Iviewlocator Viewlocator
{
Get
{
if (This._viewlocator = null)
{
This._viewlocator = new Simpleviewlocator ();
}
return this._viewlocator;
}
Set
{
This._viewlocator = value;
}
}
#endregion
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.