How to use the RazorViewEngine and a custom IView

2019-08-04 15:41发布

I've spent too much time trying to figure this out. I've not been able to work it out on my own. The following method is in my custom IView. It reads the contents of my views and returns an IView. The problem is that it won't render the razor code within the view.

    public void Render(ViewContext viewContext, System.IO.TextWriter writer)
    {
        //Load File
        var rawContents = File.ReadAllText(this.viewPhysicalPath);

        // Able to modify rawContents but Razor code isn't not being rendered.

        writer.Write(parsedcontents);
    }

This method below is part of my CustomRazorViewEngine. If have I had the implementation of base.CreateView I would think that would be where the file is read in and would be a perfect place to solve my problem. I have no idea how to find the source.

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        var view = base.CreateView(controllerContext, viewPath, masterPath);

        return view;
    }
  1. I want to read in the cshtml file for the current view. Ultimately I would like to read the cshtml file from a database source.
  2. I would like to be able to change the contents of the view. I do not have to have this feature but it would be a really nice option if I needed to dynamically change content.
  3. I want the razor code to be executed. <- This is my real problem. I can read in a cshtml file using a custom IView but doing so apparently cripples my ability to render the razor code in the page.

If anyone can help, this would be really helpful to me for work. I've been able to implement several of the options I want but when I try to combine them into a single solution I can't get it to work.

Back Story:

Where I work if we need to make a change, even a small change it requires that we create an RFC (Request for Change). Except in emergency conditions, it usually takes from 1 week to a month to get a release. On the other hand we can make database changes with in a couple of hours. I was thinking how nice it would be if I could write a process that you copy all the cshtml files into our database once after each release. That doesn't seem like it would be too difficult. I figured out how to create a custom IView that read the contents of the cshtml files. It doesn't seem to be much of a stretch to read the views in from a database instead of a file. My problem is that when I read in a cshtml file it doesn't render the razor code. Instead it's displaying the raw razor source.

My work so far has been to create a CustomViewEngine class that inherits from VirtualPathProviderViewEngine that calls my customView which successfully reads the contents of the cshtml file. If I register the engine then I get the contents displayed in the view but the razor source code is displayed on in the view.

I also created a CustomRazorViewEngine by inheriting RazorViewEngine. This works very nice and renders the razor code and I was able to limit the engine to searching for .cshtml files only which is a nice bonus but I have not found a way of pulling the view content from a data source in this class.

I've explored the inheritance tree for the view engine trying to find a better place to inject my custom code but I'm lost.

VirtualPathProviderViewEngine -> BuildManagerViewEngine -> RazorViewEngine -> IViewEngine

I have code examples of my two classes but they are fairly standard. Similar classes are available all over stackoverflow and other places.

0条回答
登录 后发表回答