controller namespaces and defining nodes in code

May 5, 2011 at 2:14 AM
Edited May 5, 2011 at 2:16 AM

Hi, not sure if this is a bug or by design but I came across this today when implementing the site map.

I have all my controllers in a seperate projects "Web.Controllers"  then in that I create a namespace for each controller and/or area.  Such as "UserController" would be in namespace "Web.Controllers.User"  then I could also have an area "SomeArea" with a bunch of controllers under "Web.Controllers.SomeArea".

I can define a node in code that's in an area with the "AreaName" attribute and this works fine. But nodes that are not inside of an Area do not work. 

The problem is with the function:


protected SiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo)

... stuff here ....

            // Determine area (will only work if controller is defined as Assembly.<Area>.Controllers.HomeController)
            string area = "";
            if (!string.IsNullOrEmpty(attribute.AreaName))
                area = attribute.AreaName;
            if (string.IsNullOrEmpty(area))
                var parts = type.Namespace.Split('.');
                area = parts[parts.Length - 2];

                var assemblyParts = type.Assembly.FullName.Split(',');

                if (type.Namespace == assemblyParts[0] + ".Controllers" || type.Namespace.StartsWith(area))
                    // Is in default areaName...
                    area = "";

.... more stuff here ...

This part does not work in my case.  If I pass "" as the area name thinking it would mean the "root" of the application it goes into the 2nd if (since it's an empty string) and tries to get the area name  from the namespace.  Since my controllers are in for example "Web.Controllers.User" 
it ends up setting the area for my controllers as "Controllers" which is not the case and the UrlHelper can't resolve the url correctly later on since there is no "controllers" area.  Nodes inside of areas work fine since I can pass AreaName "SomeArea" then it never goes into 2nd if statement.

I do see the comment up top that basically explains what I just said. But how could I map a node in code in my situation? Right now I just commented out the code that tries to determine the area from a namespace and recompiled...
Just want to know if there is some better way in case I'm not handling this correctly.


May 26, 2011 at 2:32 PM

There's no better way I'm affraid. If you do have a workarond (or a code modification that makes stuff work) feel free to post and I'll add it to the next release.