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

SiteMapTitle() and Menu() throw NullReferenceException if current node is not in the sitemap #321

Closed
infocynic opened this issue Jun 6, 2014 · 4 comments

Comments

@infocynic
Copy link

Version 4.6.4.

We're converting a site that was using a hand-coded sitemap.xml to this provider. I've got everything working except the ability to specify titles in the mvc.sitemap file and use them side by side with pages that don't belong in the sitemap, like the redirect-to-get-after-post "Form submitted OK" page.

<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home" >
    <mvcSiteMapNode title="Application" controller="Application" action="Index" />
</mvcSiteMapNode>

ApplicationController has a public ThankYou method that just returns View(). The contents of that view is a single paragraph: <p>Thanks</p>

On Home and /Application I can do this in my Layout:

<h1>@Html.MvcSiteMap.SiteMapTitle()</h1> and @Html.MvcSiteMap.Menu()

But on /Application/ThankYou, I get a NullReferenceException.

I've installed the package from NuGet and the only config that I changed was to enable the visibility filter. I commented out the appSetting, and the exception persists.

Since this is a really small site, I can work around it by adding all the nodes with visibility="!*", but I shouldn't have to do that, right?

@NightOwl888
Copy link
Collaborator

This issue was fixed in v4.4.8 (see issue #263), but if you are upgrading an existing application do note that NuGet doesn't automatically upgrade any HTML helper templates you may have customized.

You can manually merge the fixes to your templates from the commits here and here, though. Alternatively, you can just use the code from the latest templates to merge into or replace your existing ones.

You should also take care to ensure your node matches the route (including all parameters) you intend to throw at it. If your thank you page is configured in the sitemap and has a query string it needs to be taken into account in order for the node to match the request, which will make the current node a non-null value. You can use the preservedRouteParameters attribute/property to force it to match any value for a specific route key (a wildcard route value) or alternatively you can specify both the route key and value you intend to match as described in How to Make MvcSiteMapProvider Remember a User's Position, or you can use a combination of the two approaches.

Configuring your routes correctly will ensure that the current node will only be null in cases where the provided routes don't match any node, but keep in mind that you still need to fix your HTML helper templates as described above for the cases where current node is null (which can happen when the user logs out of the application, specifies unanticipated query string values, or goes to a page that is not configured in the SiteMap).

@infocynic
Copy link
Author

This is a brand-new install of 4.6.4. I uninstalled via nuget and reinstalled. As nuget is smart enough now to detect changes, it did not delete my mvc.sitemap, but everything else was deleted.

uninstall-package MvcSiteMapProvider.MVC4 -RemoveDependencies
Removing 'MvcSiteMapProvider.MVC4 4.6.4' from Website.
Successfully removed 'MvcSiteMapProvider.MVC4 4.6.4' from Website.
Removing 'MvcSiteMapProvider.Web 4.6.4' from Website.
MVC Version: 4.0.0.0
Removing config sections for MVC >= 4
Skipping 'Mvc.sitemap' because it was modified.
Successfully removed 'MvcSiteMapProvider.Web 4.6.4' from Website.
Removing 'MvcSiteMapProvider.MVC4.Core 4.6.4' from Website.
Successfully removed 'MvcSiteMapProvider.MVC4.Core 4.6.4' from Website.
Removing 'WebActivatorEx 2.0.5' from Website.
Successfully removed 'WebActivatorEx 2.0.5' from Website.
Uninstalling 'MvcSiteMapProvider.MVC4 4.6.4'.
Successfully uninstalled 'MvcSiteMapProvider.MVC4 4.6.4'.
Uninstalling 'MvcSiteMapProvider.Web 4.6.4'.
Successfully uninstalled 'MvcSiteMapProvider.Web 4.6.4'.
Uninstalling 'MvcSiteMapProvider.MVC4.Core 4.6.4'.
Successfully uninstalled 'MvcSiteMapProvider.MVC4.Core 4.6.4'.
Uninstalling 'WebActivatorEx 2.0.5'.
Successfully uninstalled 'WebActivatorEx 2.0.5'.

During the install, it detected the conflict in mvc.sitemap, and I told it not to overwrite. Skipping a bit of the log...

Adding 'MvcSiteMapProvider.Web 4.6.4' to Website.
File Conflict
File 'Mvc.sitemap' already exists in project 'Website'. Do you want to overwrite it?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "N"):n
'Mvc.sitemap' already exists. Skipping...
Successfully added 'MvcSiteMapProvider.Web 4.6.4' to Website.
Checking for .aspx extensions
Project has 1 aspx extensions
Checking for razor extensions
Project has 15 cshtml extensions
MVC Version: 4.0.0.0
Installing config sections for MVC >= 4
Adding 'MvcSiteMapProvider.MVC4 4.6.4' to Website.
Successfully added 'MvcSiteMapProvider.MVC4 4.6.4' to Website.

I then commented out of the sitemap:

<mvcSiteMapNode title="Application" controller="Application" action="Index" visibility="!*">
    <!--<mvcSiteMapNode title="Application Submitted" controller="Application" action="ThankYou" />-->
</mvcSiteMapNode>

Attempting to load /Application/ThankYou crashes with a NullReferenceException at Html.MvcSiteMap().SiteMapTitle()

@NightOwl888
Copy link
Collaborator

I have confirmed there is a problem with 4.6.4 that was introduced in the patch for #310. Version 4.6.5 will be available on NuGet shortly.

However, there is another potential issue in your project:

Project has 1 aspx extensions

If your project contains both .cshtml and .aspx files, the MvcSiteMapProvider.Web package installs the .aspx display templates in your /Views/Shared/DisplayTemplates folder. This isn't always the right decision for every project, and in your case it looks like you are using Razor for everything except one file.

So you can either remove the .aspx file from your project (assuming it can be upgraded to Razor), or you will need to manually copy in the .cshtml templates from here. Do note if you manually copy the files, you will likely have to do it again on each minor version upgrade (but not on patches) because of limitations of NuGet described in #234.

There may be no issues at all in your project, but some people have complained about weird display issues when mixing the web page views with razor views that are fixed when they change the display templates to razor.

@infocynic
Copy link
Author

Confirmed working in 4.6.5. The lone .aspx is just a legacy redirect, nothing to worry about. :)

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