tags:

views:

34

answers:

1

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.

+1  A: 

The <% %> constructs are used by ASP.NET WebForms specifically. When a WebForms based page (either a standard ASP.NET page, or an MVC view) is compiled, it creates a new assembly with classes in it, and those ASP.NET tags are used by the compiler to generate aspects of the class (such as where to do Response.Write, where to import namespaces, etc.)

What you are doing above, is directly outputting text as a view, so it won't partake in the compilation process.

The only way I can see how it would work with your above solution, is to create a new physical .aspx file in the website, so it is compiled when accessed.

What are you specifically trying to achieve, we may be able to offer suggestions...

Matthew Abbott
At the most basic level, I would like to use a string as a view. What I'm doing above is too late in the pipeline. I just want to know what the right point is to tap into and try to insert such a string.
Harpreet