Custom View Engine in ASP.NET MVC 2 does not work with Areas

Posted by mare on Stack Overflow See other posts from Stack Overflow or by mare
Published on 2010-05-06T19:01:37Z Indexed on 2010/05/06 19:08 UTC
Read the original article Hit count: 206

Filed under:
|

I used the code below so far with ASP.NET MVC v1 and v2 but when I added an Area today to my application, the controller for the area couldn't find any views in my Areas/Views/controllerView folder. It issued the very well known exception that it searched those 4 standard folders but it did not look under Areas..

How can I change the code so that it will work with Areas? Maybe an example of custom view engine under ASP.NET MVC 2 with Areas support? The information about it on the net is very scarse..

Here's the code:

public class PendingViewEngine : VirtualPathProviderViewEngine
{
    public PendingViewEngine()
    {
        // This is where we tell MVC where to look for our files. 
        /* {0} = view name or master page name       
         * {1} = controller name      */
        MasterLocationFormats = new[] {"~/Views/Shared/{0}.master", "~/Views/{0}.master"};
        ViewLocationFormats = new[]
                                {
                                    "~/Views/{1}/{0}.aspx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx",
                                    "~/Views/{1}/{0}.ascx"
                                };
        PartialViewLocationFormats = new[] {"~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.ascx"};
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        return new WebFormView(partialPath, "");
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        return new WebFormView(viewPath, masterPath);
    }
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about viewengine