views:

668

answers:

2

Some body tried Spark View Engine with asp.net mvc 2 preview 2?

I have a problem with AREAS.

It looks likes spark engine looks *.spark files inside of Views folders only instead of Areas folder in additionally.

My question is:

Somebody has information how to add it?

A: 

Spark looks for a constraint or default value key "area" in a route to determine the view location. MVC 2 area support does not add this by default, you have to do it when declaring your area:

public class AdminRoutes : AreaRegistration
{
    public override string AreaName
    {
        get { return "admin"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Dashboard", action = "Index", id = "", area = "admin" },
            new [] { "MyProject.Areas.Admin.Controllers" });
    }
}

Note the area = "admin" inside the defaults object.

roryf
i saw that in mvc2 putted area variable in dataToken dictionary. it why spark cannot find it
msony
+1  A: 

Spark will not automatically check the area views location in the current version. If you're willing to change the source (which i assume you are if you're doing mvc 2 stuff), here's the fix:

You have to modify the file src\Spark.Web.Mvc2\Descriptors\AreaDescriptorFilter.cs so that it reads as below (changes highlighted by **):

Note: I don't have the machine i did this on with me, so the slashes in the format string MIGHT need to be forward slashes.

Also, it is possible to create this class in your own code and pass it in when you register the view engine, but I don't remember the configuraiton code off the top of my head.

That's the approach I did since I wanted to modify the spark source as little as possible.

  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;
    }
}
midas06
in MVC2 moved area to DataTokens, it mean that u must change this row:context.RouteData.Values.TryGetValue("area", out value)to this:context.RouteData.DataTokens.TryGetValue("area", out value)
msony
I'm looking at the code right now. The code above is working for me right now, running on asp.net mvc 2 beta (the latest release as of today).
midas06