软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP.NET > ASP.NET MVC 1.0 NVelocityViewEngine

ASP.NET MVC 1.0 NVelocityViewEngine

NVelocity Template Engine 是个好东西,可惜很久没有新版本了。以下代码可自由使用,如果你有时间完成一个功能完善的版本,请发给我一份,谢谢。

using NVelocity;
using NVelocity.App;
using NVelocity.Context;

public class NVelocityViewEngine : VirtualPathProviderViewEngine
{
public NVelocityViewEngine()
{
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.htm",
"~/Views/{1}/{0}.html",
"~/Views/Shared/{0}.htm",
"~/Views/Shared/{0}.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);
}
}

试试看效果。


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>

当然,我们也可以将其注册成默认视图引擎,免得每次要创建 IView 传递给 View()。

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

RegisterRoutes(RouteTable.Routes);
}
}

或者写一个 IResultFilter,标注在需要使用 NVelocityViewEngine 的 ActionMethod 上。

public class NVelocityViewAttribute : ActionFilterAttribute
{
public NVelocityViewAttribute()
{
}

public NVelocityViewAttribute(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);
}
}
}

现在使用起来更加方便了。

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

return View();
}

还可以手工指定一个模板。

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

return View();
}

点击下载 NVelocity.dll

    相关阅读
    网友评论
    栏目导航
    推荐软件