在有些項目需求上或許須要根據模板生產靜態頁面,那麼你同樣能夠用Razor語法去直接解析你的頁面從而把解析的頁面生成靜態頁,這樣的使用場景不少,不限於生成靜態頁面,視圖引擎爲咱們提供了模型到視圖的代碼或文本生成的能力。html
本文章中採用的是ASP.NET Core MVC原生的方法;固然我在YOYOFx中也實現了這種視圖引擎 ( https://github.com/maxzhang1985/YOYOFx/blob/master/AspNetCore/YOYO.AspNetCore.ViewEngine.Razor ) 。git
固然在MVC 四、5的時代,咱們也使用過如RazorEngine這樣的第三方的視圖引擎,那時候MVC中的Razor與框架耦合的比較緊密,第三方開源組件幫咱們實現了在任意項目中使用Razor渲染視圖爲文本的方式;可是在.NET Core中 RazorEngine開源組件並無移植過來,接下來咱們仍是要自食其力實現一個吧。github
其實在ASP.NET Core MVC中給咱們提供了這樣的方法,只是用起來不太方便,咱們來對它進行一下封裝。框架
框架早已經爲咱們提供了視圖渲染的接口 IRazorViewEngine 經過它的FindView方法來查找視圖及.cshtml文件,固然查找方法也與MVC中的視圖路徑規則是對應的,原本就是一個東西嘛:) async
咱們來一步步實現,首先咱們創建一個視圖渲染器的接口:ide
public interface IViewRenderService { Task<string> RenderToStringAsync(string viewName, object model); }
而後是實現類了,代碼很簡單一看就懂了:學習
public class ViewRenderService : IViewRenderService { private readonly IRazorViewEngine _razorViewEngine; private readonly ITempDataProvider _tempDataProvider; private readonly IServiceProvider _serviceProvider; public ViewRenderService(IRazorViewEngine razorViewEngine, ITempDataProvider tempDataProvider, IServiceProvider serviceProvider) { _razorViewEngine = razorViewEngine; _tempDataProvider = tempDataProvider; _serviceProvider = serviceProvider; } public async Task<string> RenderToStringAsync(string viewName, object model) { var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); using (var sw = new StringWriter()) { var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); if (viewResult.View == null) { throw new ArgumentNullException($"{viewName} does not match any available view"); } var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), sw, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return sw.ToString(); } } }
咱們使用一個簡單的例子來講明如何使用咱們上面那個渲染器。spa
public class MyUserViewModel { public string Name { set; get; } }
@model WebApplication6.Models.MyUserViewModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Render view to string</title> </head> <body> <div> @Model.Name </div> </body> </html>
public class HomeController : Controller { private IViewRenderService _viewRenderService; public HomeController(IViewRenderService viewSendeRenderService) { _viewRenderService = viewSendeRenderService; } public async Task<IActionResult> Index() { var user = new MyUserViewModel { Name = "hello world" }; var result = await _viewRenderService.RenderToStringAsync("Home/Template1", user); return Content(result); } }
public void ConfigureServices(IServiceCollection services) { services.AddScoped<IViewRenderService, ViewRenderService>(); services.AddMvc(); }
最後我只能說在ASP.NET Core中是萬物皆DI啊,其實ASP.NET Core中的實現早就爲咱們想好了這些功能,只是改變了使用方式。code
GitHub:https://github.com/maxzhang1985/YOYOFx 若是覺還能夠請Star下, 歡迎一塊兒交流。htm
.NET Core 開源學習羣: 214741894