_ViewStart stops working after creating a custom razor viewer

I created a special razor viewer mechanism that extends RazorViewEngine. Then, unexpectedly, I cannot access the variables defined in _ViewStart.cshtml in the / Views folder, which usually works on the default project, initialized by the VS2010 MVC3 template.

My question is, how do I enable _ViewStart in razor view in a non-standard way?

+4
source share
1 answer

Without seeing my code, I can’t say what is going wrong, but after looking at the source for RazorViewEngine , I think there may be an idea that you are not doing it.

Here's what the CreateView method looks like:

  protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { var view = new RazorView(controllerContext, viewPath, layoutPath: masterPath, runViewStartPages: true, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator) { DisplayModeProvider = DisplayModeProvider }; return view; } 

You can see that they pass true in for the runViewStartPages argument in the RazorView constructor. Looking at the source of the RenderView method in the RazorView class shows that this boolean parameter is used to create StartPageLookupDelegate , which is responsible for finding the _ViewStart file and compiling it into the execution hierarchy.

 WebPageRenderingBase startPage = null; if (RunViewStartPages) { startPage = StartPageLookup(webViewPage, RazorViewEngine.ViewStartFileName, ViewStartFileExtensions); } webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext, page: null, model: null), writer, startPage); 

So, this means that you are probably doing one of two things:

  • Overriding the CreateView RazorViewEngine method and not initializing a RazorView described above.
  • Overriding the RenderView RazorView method without creating StartPageLookupDelegate and passing it to the ExecutePageHierarchy method.

We hope this helps you find the right way to find a solution!

0
source

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


All Articles