Understanding asp.net mvc IView and IView.Render

Posted by Harpreet on Stack Overflow See other posts from Stack Overflow or by Harpreet
Published on 2010-06-17T04:29:21Z Indexed on 2010/06/17 4:33 UTC
Read the original article Hit count: 640

Filed under:

I'm trying to devise a method of doing VERY simple asp.net mvc plugins but mostly I'm trying to understand how view rendering works.

I've distilled my problem down to this

public class CustomView : IView
{
    public void Render(ViewContext viewContext, TextWriter writer)
    {
        writer.Write( /* string to render */);
    }
}

Now, within that write method I can render any string to the view but when I put a line of code in there wrapped with <% %> it renders the code to the view literally rather than parsing it and executing it. I've tried adding things like <% @Page ... to the beginning of the string and it just renders that literally as well.

Among many attempts I'm currently calling it this way within a controller action:

...
CustomView customView = new CustomView();

ViewResult result = new ViewResult();
result.View = customView;
result.ViewName = "Index.aspx";
result.MasterName = "";
return result;

What am I missing or doing wrong that this won't work? The ViewResult seems to have the WebFormViewEngine in its ViewEngines collection.

I just want to understand this and after stripping it down to what I think should be the minimum it doesn't behave as I think it should. I'm guessing some other part of the machinery is involved/missing but I can't figure out what.

© Stack Overflow or respective owner

Related posts about asp.net-mvc