maartenba/MvcSiteMapProvider — Issue #15: Empty RouteData when using non MVC handler
Author: minispam
Created: 2012-01-12 21:44:50+00:00
State: open

I had a rather unusual case scenario where I used MapPageRoute tu simulate " restul " URL while keeping my existing WebForms pages. While creating my sitemap config file I discovered that all my parameters were lost ( besides " controller " and " action " ) even though I had them in the preserve list ( also, could you tell what's the difference between the preserve list and the inherit list... It seems to work whether I put one or the other. Maybe a little case scenario would be helpful ).

I discovered that if the handler wasn't of type " MvcHandler ", the " RouteData " parameter of the " new RequestContext " object was just a plain " new RouteData() ". The problem was that even when mapping to a WebForms page, I still needed the values of the parameters in the preserve list, but they were always empty so the generated URL was wrong, or worse, I would get a " NullReferenceException " in the " DefaultSieMapProvider ".

I modified the " UrlHelper " property to put the populated dictionary of the " Page " handler instead of the empty one. Here is a revised version of the property:

protected UrlHelper UrlHelper
{
get
{
if (HttpContext.Current.Items[UrlHelperCacheKey] == null)
{
RequestContext ctx;
if (HttpContext.Current.Handler is MvcHandler)
ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
else if (HttpContext.Current.Handler is Page)
//ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());
ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), ((Page)HttpContext.Current.Handler).RouteData);
else
ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());
HttpContext.Current.Items[UrlHelperCacheKey] = new UrlHelper(ctx);
}
return (UrlHelper)HttpContext.Current.Items[UrlHelperCacheKey];
}
}

Now everything works as expected