RazorEngine with layout: object reference not set to object instance

I have the following code:

public string View(string view, object model) { var template = File.ReadAllText(HttpContext.Current.Request.MapPath(@"~\Views\PublishTemplates\" + view + ".cshtml")); if (model == null) { model = new object(); } return RazorEngine.Razor.Parse(template, model); } 

and I use the following form

 @model NewsReleaseCreator.Models.NewsRelease @{ Layout = "~/Views/Shared/_LayoutBlank.cshtml"; } @Model.Headline 

I get:

[NullReferenceException: the reference to the object is not set to the instance of the object.] RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run (context ExecuteContext) in c: \ Users \ Matthew \ Documents \ GitHub \ RazorEngine \ src \ Core \ RazorEngine.Core \ Templating \ TemplateBase.cs: 139

If I delete the Layout Line, it works fine

My layout

 <!DOCTYPE html> <html> <head> @RenderSection("MetaSection", false) <title>@ViewBag.Title</title> @RenderSection("HeaderSection", false) </head> <body> @RenderBody() </body> </html> 

Thoughts?

+6
source share
2 answers

I ended up not using the Razor Engine

My solution requires a controller context, so I just use the one that was called by the controller. So in my controller

 InstanceOfMyClass.ControllerCurrent = this 

And in MyClass

  public string RenderViewToString(string viewName, object model, string layoutName) { ControllerCurrent.ViewData.Model = model; try { using (StringWriter sw = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerCurrent.ControllerContext, viewName, layoutName); ViewContext viewContext = new ViewContext(ControllerCurrent.ControllerContext, viewResult.View, ControllerCurrent.ViewData, ControllerCurrent.TempData, sw); viewResult.View.Render(viewContext, sw); return sw.GetStringBuilder().ToString(); } } catch (Exception ex) { return ex.ToString(); } } 
0
source

I looked at the sources of TemplateBase.cs ( https://github.com/Antaris/RazorEngine/blob/master/src/Core/RazorEngine.Core/Templating/TemplateBase.cs ):

 line 139: return layout.Run(context); 

NullReferenceException is possible if the "layout" variable is null. Well, what is a "layout"?

 line 133: var layout = ResolveLayout(Layout); 

Go deeper ( https://github.com/Antaris/RazorEngine/blob/master/src/Core/RazorEngine.Core/Templating/TemplateService.cs ):

 public ITemplate Resolve(string cacheName, object model) { CachedTemplateItem cachedItem; ITemplate instance = null; if (_cache.TryGetValue(cacheName, out cachedItem)) instance = CreateTemplate(null, cachedItem.TemplateType, model); if (instance == null && _config.Resolver != null) { string template = _config.Resolver.Resolve(cacheName); if (!string.IsNullOrWhiteSpace(template)) instance = GetTemplate(template, model, cacheName); } return instance; } 

And I see here that NullReference is possible if _config.Resolver is null. Check your resolver.

+3
source

Source: https://habr.com/ru/post/946547/


All Articles