Reputation: 18220
Does anyone know how to get the current folder of the view being called? I'm using AreasLib as well, so I need to get it dynamically without prefixing paths. So for example, if I do http://localhost/myarea/mycontroller/myaction
it will give me the path of Project/Areas/myarea/Views/mysubsystem
. So yeah, it needs to be the folder that the view is in.
EDIT: It's either that, or editing MvcSiteMap source code to deal with AreasLib. I'm not really equipped with the skills, or the time more importantly to deal with it.
FINAL
It's not pretty, but I had no choice whatsoever. I'm using partial views to render the menu components using the following helper to find the correct one. I'm not dealing with missing menu.ascx files in this version, but it's assumed there will always be one for now.
using System;
using System.Web;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
namespace Intranet.Helpers
{
public static class StaticMenuHelper
{
public static string StaticMenu(this HtmlHelper helper, string area, string controller)
{
if (area.Equals("root"))
{
return "/Views/" + controller + "/menu.ascx";
}
else
{
return "/Areas/" + area + "/Views/" + controller + "/menu.ascx";
}
}
}
}
And the following in my Site.Master
.
<div id="menu">
<% Html.RenderPartial(Html.StaticMenu(ViewContext.RouteData.Values["area"].ToString(), ViewContext.RouteData.Values["controller"].ToString())); %>
</div>
Until MvcSiteMap has matured into something which can deal with AreasLib then I'll have to use this. I really, really don't have the time to be modifying that lib.
Thanks for all the help.
Upvotes: 2
Views: 917
Reputation: 180777
Well, it seem to me that, if you are calling into the view from a particular controller, it would be pretty straightforward to build the path from what you already know. If you need it in the view, you can just pass it in using a ViewData entry.
So, if you do
http://localhost/myarea/mycontroller/myaction
then the controller you are calling into already knows myarea, mycontroller, and myaction. I assume you are building either a partial view or an Html helper method to handle the menu, so when you render your view pass myarea, mycontroller, and myaction into the view as ViewData variables. In your view, pass those variables into your partial view or Html helper method.
Or, you can build your path from myarea, mycontroller, and myaction, and push the whole path into your view instead.
Usage:
<%= Html.Menu(ViewData["Area"], ViewData["Controller"], ViewData["Action"]) %>
Example function to calculate path:
string GetPath(string area, string controller, string action)
{
return "/project/areas/" + area + "/" + controller + "/" + action
}
EDIT: As Craig Stuntz pointed out, you can get the route values in a view (via ViewContext) without having to put them into ViewData first. That means you can build your path directly in your Menu extension method, provided you can get area the same way. I'm not sure whether Phil's stuff supports that or not.
ViewContext.RouteData.Values["controller"].ToString()
ViewContext.RouteData.Values["action"].ToString()
Upvotes: 3
Reputation: 126547
Is that not the same as the view's namespace? That's easy enough to examine from the view itself.
Upvotes: 1