ASP. net mvc 1.0 NVelocityViewEngine

Source: Internet
Author: User

NVelocity Template Engine is a good thing, but it has not been a new version for a long time. The following code is free to use. If you have time to complete a fully functional version, please send me a copy. Thank you.

Using NVelocity;
Using NVelocity. App;
Using NVelocity. Context;

Public class NVelocityViewEngine: VirtualPathProviderViewEngine
{
Public NVelocityViewEngine ()
{
ViewLocationFormats = new []
{
"~ /Views/{1}/02.16.htm ",
"~ /Views/{1}/02.16.html ",
"~ /Views/Shared/share0).htm ",
"~ /Views/Shared/custom 02.16.html ",
};
}

Protected override IView CreatePartialView (ControllerContext controllerContext, string partialPath)
{
Throw new NotImplementedException ();
}

Protected override IView CreateView (ControllerContext controllerContext, string viewPath, string masterPath)
{
Return new NVelocityView (controllerContext, viewPath );
}
}

Public class NVelocityView: IView
{
Private static VelocityEngine engine;
Private string viewPath;
Private ControllerContext controllerContext;

Static NVelocityView ()
{
Engine = new VelocityEngine ();
Engine. SetProperty ("resource. loader", "file ");
Engine. SetProperty ("file. resource. loader. path", HttpContext. Current. Server. MapPath ("~ "));
// Engine. SetProperty ("file. resource. loader. cache", true );
// Engine. SetProperty ("file. resource. loader. modificationCheckInterval", 10L); // seconds
Engine. SetProperty ("input. encoding", "UTF-8 ");
Engine. Init ();
}

Public NVelocityView (ControllerContext controllerContext, string viewPath)
{
This. controllerContext = controllerContext;
This. viewPath = viewPath. StartsWith ("~ ")? ViewPath. Substring (1): viewPath;
}

Public void Render (ViewContext viewContext, System. IO. TextWriter writer)
{
Var context = new VelocityContext ();
Var template = engine. GetTemplate (viewPath );

Context. Put ("ViewData", viewContext. ViewData );
Context. Put ("TempData", viewContext. TempData );

Template. Merge (context, writer );
}
}

Try it.


Public class TestController: Controller
{
Public ActionResult Index ()
{
ViewData ["user"] = new
{
Name = "Tom ",
Age = 13
};

Var view = new NVelocityView (this. ControllerContext, "/Views/Test/Index.htm ");
Return View (view );
}
}

/Views/Test/Index.htm
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
$ ViewData. User. Name <br/>
</Body>
</Html>

Of course, we can also register it as the default View engine, so that you do not need to create an IView every time and pass it to View ().

Public class MvcApplication: System. Web. HttpApplication
{
Protected void Application_Start ()
{
ViewEngines. Engines. Clear ();
ViewEngines. Engines. Add (new NVelocityViewEngine ());

RegisterRoutes (RouteTable. Routes );
}
}

Or write an IResultFilter, which is marked on the ActionMethod that requires NVelocityViewEngine.

Public class nvelocityviewattriter: ActionFilterAttribute
{
Public NVelocityViewAttribute ()
{
}

Public nvelocityviewattripath (string viewPath)
{
This. ViewPath = viewPath;
}

Public string ViewPath {get; set ;}

Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
If (String. IsNullOrEmpty (ViewPath ))
{
Foreach (var ext in new [] {"html", "htm "})
{
ViewPath = String. Format ("~ /Views/{0}/{1}. {2 }",
FilterContext. RouteData. GetRequiredString ("controller "),
FilterContext. RouteData. GetRequiredString ("action "),
Ext );

If (System. IO. File. Exists (HttpContext. Current. Server. MapPath (ViewPath) break;
}
}

Var result = filterContext. Result as ViewResult;

If (result! = Null)
{
Result. View = new NVelocityView (filterContext, ViewPath );
}
}
}

Now it is more convenient to use.

[NVelocityView]
Public ActionResult Index ()
{
ViewData ["user"] = new
{
Name = "Tom ",
Age = 13
};

Return View ();
}

You can also manually specify a template.

[NVelocityView ("/Views/Test/Index2.htm")]
Public ActionResult Index ()
{
ViewData ["user"] = new
{
Name = "Tom ",
Age = 13
};

Return View ();
}

Click to download NVelocity. dll

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.