maartenba / MvcSiteMapProvider

An ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework.
Microsoft Public License
537 stars 220 forks source link

SiteMapPath not taking cookie-less uri into account #407

Closed vtherry closed 8 years ago

vtherry commented 8 years ago

Hi,

we are facing an issue while upgrading my project from MVC3 to MVC5 (and so MvcSiteMapProvider 2.x to MvcSiteMapProvider 4.x).

In our project, we are obliged to use cookie-less uris and everything was working fine with MvcSiteMapProvider 2.x. Our SiteMap path was displaying correctly with the session ID in the url but when upgrading to MVC5 and MvcSiteMapProvider 4.x, the session ID is not there now.

We have also lost our preserved parameters :(

I was able to reproduce it in a very simple project. Here is a gist with my configuration : https://gist.github.com/vtherry/2403b94e53c6a96c8bda

Please let met know if I am using the framework the wrong way or if cookie-less sessions are not supported anymore but there is still the issue with preserved parameters not being displayed in uris :(

Thank you for your support ! Vincent

NightOwl888 commented 8 years ago

Hello.

The official stance of Microsoft is that cookieless sessions are not supported in MVC.

If you still want to go down that road, the thing that changed which causes the cookieless sessions not to work is that a fake HttpContext based on the home page is used to resolve the URLs. This keeps values from bleeding through unintentionally from the current context into the generated URLS, which some people find confusing (see #213).

You can use the actual HttpContext of the current page by setting the includeAmbientValuesInUrl attribute/property of the node, which allows the session state key to be written to the URL. Unfortunately, this has to be applied to every node in order to make it work, though.

v2 and v3 had a bug that many people were relying on in their configuration - route values that were written on a request went into the primary cache. This made it seem like it saved the ids for the current user across requests when navigating around the site. However, the cache was shared between all users so the next user that came along would overwrite the RouteValue settings. This has been fixed in v4, but is also a source of confusion because configurations that seemed to work before no longer work.

I have created a demo project based off of your configuration showing one way you could get your breadcrumbs to work using Preserved Route Parameters. There is also a post titled How to Make MvcSiteMapProvider Remember a User's Position that goes into some detail about how this works (which has more demos).

vtherry commented 8 years ago

Hi,

Thank you for your quick answer ! It is much appreciated !

With your comments I was able to understand the behavior of the preservedRouteParameters attribute. I was using ID properties for different entities and trying to remember them in the breadcrumb, but only the ID in the uri was showing in node's urls. Now I got it and I pass my parameters to the actions keeping names identical.

The SessionID is working well with parameter includeAmbientValuesInUrl now.

So ... many thanks ! :) I was able to fully upgrade my project to MVC5. Vincent