Loading Spark Views in Area MVC 2.0

8 views
Skip to first unread message

Jaikishan Jalan

unread,
Jul 9, 2010, 8:05:34 PM7/9/10
to spar...@googlegroups.com
Hello,

I am working on a MVC 2.0 application in which I have an area named Product. Both the root and the product area defaults the request to Home controller and Index view. When I hit the root of the application it correctly displays the view located at the root ~/Views/Home/Index.spark. However, when I hit http://xxx/product, it does hit the Home Controller located in Area.Product.Controller namespace, but it keep rendering the Index view located at the root i.e. ~/Views/Home/Index.spark. I was expecting it to render the view at ~/Areas/Product/Views/Home/Index.spark.

I am working with latest Spark 1.1 bits. I am wondering if I this is a bug or am I doing anything wrong ?

In my global.asax file, I have the route :

routes.MapRoute(
                "ParentHome", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "Blah.Controllers" }
            );

In my product area registerarea method, I have

context.MapRoute(
                "Product_default",
                "Product/{controller}/{action}/{id}",
                new {controller="Home", action = "Index", id = UrlParameter.Optional }
            );

--
Thanks,
Jaikishan

Jaikishan Jalan

unread,
Jul 9, 2010, 10:06:54 PM7/9/10
to spar...@googlegroups.com
Based on my debugging, it seems that Spark was looking for the view at : ~/Views/Product/Home/Index.spark. Since this view is actually located at (~/Areas/Product/Views/Home/Index.spark), it is unable to find this view and therefore goes to parent ~/Views/Home/Index.spark by default. One way to solve would be create VirtualPathProvider for every file request starting with ~/Views/<AREANAME>. This would not be ideal solution though for obvious performance bottle necks and not straight forward approach. Second option would be to modify the PotentialLocations function inside AreaDescriptor to generate the path properly and then changing a little bit of logic of HasView function in VirtualPathProviderViewFolder. This would mean to generate the dll again but I want to keep in sync with the distributed source code out there and do not want to create my own customized view engine.

Please let me know if there is another way to solve this problem.
--
Thanks,
Jaikishan

Matt Dotson

unread,
Jul 9, 2010, 11:16:14 PM7/9/10
to spar...@googlegroups.com, spar...@googlegroups.com
I would love to see this fixed in spark.  +1 for fixing potentiallocations.  Using webforms rendering engine in my admin area because of this bug.
--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.

Rob G

unread,
Jul 10, 2010, 1:59:37 AM7/10/10
to spar...@googlegroups.com
Hi Jaikishan,

Sounds like you're almost already there with a fix. Why not put the code in place, and test your areas. If that works, and you run the comprehensive suite of tests in the source tree and nothing breaks unexpectedly, then I'm pretty sure a pull request to the main repo will get accepted and rolled into the main code base.

Doesn't sound like a massive change - but a valuable one!

All the best,
Rob

--

Adam Schroder

unread,
Jul 10, 2010, 5:39:28 AM7/10/10
to spar...@googlegroups.com
I have code that enables this. Will post in a few hours, when I get home.

Adam

Adam Schroder

unread,
Jul 10, 2010, 7:47:08 AM7/10/10
to spar...@googlegroups.com
I have these two classes and then I just call SparkConfiguration.Configure() in my global.asax.cs on app start.
This will add register the view engine with Asp.net MVC as well.

Please not that this is the code for Asp.net MVC 2.

    public static class SparkConfiguration
    {
        public static void Configure()
        {
            var settings = new SparkSettings();
            settings.SetAutomaticEncoding(true);

            settings
                .AddNamespace("System")
                .AddNamespace("System.Collections.Generic")
                .AddNamespace("System.Linq")
                .AddNamespace("System.Web.Mvc")
                .AddNamespace("System.Web.Mvc.Html")
                .AddNamespace("Microsoft.Web.Mvc");

            settings
                .AddAssembly("Microsoft.Web.Mvc")
                .AddAssembly("Spark.Web.Mvc")
                .AddAssembly("System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
                .AddAssembly("System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");


            Spark.Web.Mvc.DefaultDescriptorBuilder builder = new DefaultDescriptorBuilder();
            builder.Filters.Add(new AreaDescriptorFilter());
            var services = SparkEngineStarter.CreateContainer(settings);
            services.SetServiceBuilder<Spark.Web.Mvc.IDescriptorBuilder>(c => builder);
            SparkEngineStarter.RegisterViewEngine(services); 
        }
    }

    public class AreaDescriptorFilter : DescriptorFilterBase
    {
        private const string AreaPathFormatString = "~\\Areas\\{0}\\Views";

        public override void ExtraParameters(
            ControllerContext context,
            IDictionary<string, object> extra)
        {
            object value;
            if (context.RouteData.Values.TryGetValue("area", out value))
                extra["area"] = value;
        }

        public override IEnumerable<string> PotentialLocations(
            IEnumerable<string> locations,
            IDictionary<string, object> extra)
        {
            string areaName;
            return TryGetString(extra, "area", out areaName)
                       ? locations.Select(x => Path.Combine(string.Format(AreaPathFormatString, areaName), x)).Concat(locations)
                       : locations;
        }
    }

Enjoy.

Adam

Jaikishan Jalan

unread,
Jul 10, 2010, 2:58:53 PM7/10/10
to spar...@googlegroups.com
Thanks Adam for the code. This is what exactly needs to happen. Append the areas path in the default AreaDescriptorFilter class in the project. Rob, I made the changes in the source and tested it with areas and runs just fine ( I still need to run test cases though, they dont seems to be loading in my visual studio solution). What is the process to incorporate the changes into the main branch ?
Thanks,
Jaikishan

Rob G

unread,
Jul 10, 2010, 3:08:59 PM7/10/10
to spar...@googlegroups.com
I presume you've created a fork of the main repo and can push your changes up? 

Adam - have you perhaps done that?

If so, you can do a pull request from github to have them merged in as a patch by Louis (or one of the main commiters).

If not, you can email me the changed files (and full paths) and I'll merge them into my fork and run the tests. If they pass, then they'll go in with my next pull request but I wouldn't say that's the normal procedure, the process above is the norm.

Hope that helps,
Rob

Jaikishan Jalan

unread,
Jul 10, 2010, 4:41:36 PM7/10/10
to spar...@googlegroups.com
Thanks rob. I will try to fork the branch and submit my changes.

Sent from my Verizon Wireless Phone



From: Rob G <robertg...@gmail.com>
Sent: Saturday, July 10, 2010 12:08 PM
To: spar...@googlegroups.com
Subject: Re: Loading Spark Views in Area MVC 2.0


I presume you've created a fork of the main repo and can push your changes up? 

Adam - have you perhaps done that?

If so, you can do a pull request from github to have them merged in as a patch by Louis (or one of the main commiters).

If not, you can email me the changed files (and full paths) and I'll merge them into my fork and run the tests. If they pass, then they'll go in with my next pull request but I wouldn't say that's the normal procedure, the process above is the norm.

Hope that helps,
Rob

On Sat, Jul 10, 2010 at 7:58 PM, Jaikishan Jalan <jai...@gmail.com> wrote:
Thanks Adam for the code. This is what exactly needs to happen. Append the areas path in the default AreaDescriptorFilter class in the project. Rob, I made the changes in the source and tested it with areas and runs just fine ( I still need to run test cases though, they dont seems to be loading in my visual studio solution). What is the process to incorporate the changes into the main branch ?

On Sat, Jul 10, 2010 at 4:47 AM, Adam Schroder <adamsc...@gmail.com> wrote:
I have these two classes and then I just call SparkConfiguration.Configure() in my global.asax.cs on app start.
This will add register the view engine with Asp.net MVC as well.

Please not that this is the code for Asp.net MVC 2.


[The entire original message is not included]

Louis DeJardin

unread,
Jul 11, 2010, 12:43:22 AM7/11/10
to spar...@googlegroups.com

Added github/RobertTheGrey as collaborator on github/loudej/spark – not exactly sure what that means to github, but I’m assuming you’ll be able to push changes to that central area most people forked from and ci is pointed to.

 

 

 

From: spar...@googlegroups.com [mailto:spar...@googlegroups.com] On Behalf Of Rob G
Sent: Saturday, July 10, 2010 12:09 PM
To: spar...@googlegroups.com
Subject: Re: Loading Spark Views in Area MVC 2.0

 

I presume you've created a fork of the main repo and can push your changes up? 

Reply all
Reply to author
Forward
0 new messages