Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do I have to have a Mvc.sitemap or can I drive SiteMapPathHelper using Attributes only ? #197

Closed
johnkattenhorn opened this issue Aug 3, 2013 · 3 comments

Comments

@johnkattenhorn
Copy link

I'd like to use the SiteMapPathHelper by decorating the Controller Actions Only, but I don't seem to make it work correctly and it loses the 'root' node.

On HomeController I have

public class HomeController : Controller
{

    //
    // GET: /Home/
    [MvcSiteMapNode(Title = "Home", Key = "Home")]
    public ActionResult Index()
    {

        //TODO: Create some logic which looks up where the user left off or defaults to the below

        return RedirectToAction("Index","Dashboard");
    }

}

On dashboard I have

public class DashboardController : Controller
{
//
// GET: /Dashboard/
[MvcSiteMapNode(Title = "Dashboard", Key = "Dashboard", ParentKey = "Home")]
public ActionResult Index()
{
return View();
}

}

If I set a breakpoint inside SiteMapPathHelperModel the Model seems to be empty, I can make it work by added nodes to the sitemap file but that's not the way I want to do; any ideas ?

Thanks

John

@NightOwl888
Copy link
Collaborator

In theory it should work, but it is not something I have tested. However in order to make it work you need to:

  • Make sure you are using version 4.0.5, there was a recent fix to ensure the root node is added to the sitemap.
  • Use an external DI container.
  • Setup the configuration without an XmlSiteMapBuilder.
  • Setup one and only one node with no parent key - that will be used as your root node.
// Original DI configuration
var builder = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
    .EnumerableOf<ISiteMapBuilder>().Contains(y =>
    {
      y.Type<XmlSiteMapBuilder>()
          .Ctor<ISiteMapXmlReservedAttributeNameProvider>().Is(reservedAttributeNameProvider)
          .Ctor<IXmlSource>().Is(xmlSource);
      y.Type<ReflectionSiteMapBuilder>()
          .Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
          .Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
      y.Type<VisitingSiteMapBuilder>();
    });

// New DI Configuration
var builder = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
    .EnumerableOf<ISiteMapBuilder>().Contains(y =>
    {
      y.Type<ReflectionSiteMapBuilder>()
          .Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
          .Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
      y.Type<VisitingSiteMapBuilder>();
    });

I am showing you the StructureMap configuration, but you should be able to work this out with your preferred DI container. Basically you need to do this to ensure the root node is set by the ReflectionSiteMapBuilder rather than the XmlSiteMapBuilder and also so your setup won't be looking for a .sitemap file.

Give it a try and let me know if there are any problems - if this configuration is not supported currently, it definitely is something that should be. It might help if you try it in a demo project first before doing it in your real project. That way if there are bugs you can just post your demo project on GitHub so we can analyze what is going on with your exact configuration.

@NightOwl888
Copy link
Collaborator

FYI - I did a quick test and it seems to work fine when set up the way I describe it above. I did have to add a few steps to make it complete:

  • DI config: Change the cache dependency from a RuntimeFileCacheDependency to a NullCacheDependency.
  • DI config: Remove the xmlSource and line that resolves the absolute file path for Mvc.sitemap.
  • Delete the Mvc.sitemap file and MvcSiteMapProvider.xsd file.
  • Remove the XML validation code from App_Start\MvcSiteMapProviderConfig.cs.

All DI configuration is done in DI\StructureMap\Registries\MvcSiteMapProviderRegistry.cs.

Here is the demo project I created: https://github.com/NightOwl888/MvcSiteMapProvider_197. I commented the lines I removed so you can more easily see what changed from the default configuration. I started by using the default MVC internet template and installing the MvcSiteMapProvider.MVC4.DI.StructureMap package only.

@johnkattenhorn
Copy link
Author

Thanks so much – I didn’t expect such a quick response; worked a treat.

I happen to be using structuremap so your example was really easy to follow.

All the best

John Kattenhorn

From: NightOwl888 [mailto:[email protected]]
Sent: 03 August 2013 14:47
To: maartenba/MvcSiteMapProvider
Cc: John Kattenhorn
Subject: Re: [MvcSiteMapProvider] Do I have to have a Mvc.sitemap or can I drive SiteMapPathHelper using Attributes only ? (#197)

FYI - I did a quick test and it seems to work fine when set up the way I describe it above. I did have to add a few steps to make it complete:

  • DI config: Change the cache dependency from a RuntimeFileCacheDependency to a NullCacheDependency.
  • DI config: Remove the xmlSource and line that resolves the absolute file path for Mvc.sitemap.
  • Delete the Mvc.sitemap file and MvcSiteMapProvider.xsd file.
  • Remove the XML validation code from App_Start\MvcSiteMapProviderConfig.cs.

All DI configuration is done in DI\StructureMap\Registries\MvcSiteMapProviderRegistry.cs.

Here is the demo project I created: https://github.com/NightOwl888/MvcSiteMapProvider_197. I commented the lines I removed so you can more easily see what changed from the default configuration. I started by using the default MVC internet template and installing the MvcSiteMapProvider.MVC4.DI.StructureMap package only.


Reply to this email directly or view it on GitHubhttps://github.com//issues/197#issuecomment-22055147.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants