Geeks With Blogs
Aftab Qauder

Part 3 – Creating the view engine

[Download the code as a .Zip here]





Looking at the precompiled view from earlier we can see it derives from WebViewPage, however the view engine wants a view that implements IView. To get around this we create a wrapper class for the view. As you can see we pass the view into the constructor.

The code below has been taken from

Here

but I've stripped it down for simplicity. You may wish to refer back to Chris van de Steeg's code for a more robust example.

The most important part of the example below is the Render method which is what MVC will call when it attempts to use the view.





Now we will look at our custom view engine. To handle out views we create a ViewManager, this simply contains a list of views.We can use this ViewManager resolve the correct view by name.



When MVC wants a view it will call CreateView on the View Engine. As you can see all we are doing is calling the ViewManager to find the view by name and returning a wrapper object to MVC.  MVC will then call the Render method on the view.




If we look at the method FindView we can see what’s being passed in as a parameter. The naming convention here is being defined by the ViewLocationFormats in our ViewEngine.


In this example we’re keeping it quite simple and just using the view name to return a view from the views list. In a real situation it would be quite probable that more than one controller would have  a methodwith the same name on it so you would probably have to take the controller into account as well.

 

Finally navigating to the correct controller and view will render the view to the browser.

Posted on Sunday, June 12, 2011 7:50 PM | Back to top


Comments on this post: MVC 3 and Castle Windsor: A modular approach - Part 3

# re: MVC 3 and Castle Windsor: A modular approach - Part 3
Requesting Gravatar...
Thank you that was helpful. I am working on a way to create plugins for a MVC3 CMS and your example helped me understand the use of Castle Installers.
Left by Matthew on Nov 20, 2011 12:08 AM

# re: MVC 3 and Castle Windsor: A modular approach - Part 3
Requesting Gravatar...
I am studying the area, can you give me a source copy? my email: liuzs@sina.com. thanks
Left by noah on Jun 11, 2012 11:15 AM

# re: MVC 3 and Castle Windsor: A modular approach - Part 3
Requesting Gravatar...
I get this error:
The IControllerFactory 'MvcApplication1.Controllers.CustomControllerFactory' did not return a controller for the name 'HelloWorld'.
Left by nasrin on Dec 16, 2013 7:02 PM

Your comment:
 (will show your gravatar)


Copyright © aftabq | Powered by: GeeksWithBlogs.net