Null reference error from using URL routing
I implemented the URL routing example mentioned in http://www.sitefinity.com/devnet/forums/sitefinity-4-x/developing-with-sitefinity/url-routing.aspx. I just upgraded from Sitefinity 4.2 to 4.4, and now I'm getting the following error for any URL using the custom route:
[NullReferenceException: Object reference not
set
to an instance of an
object
.]
Telerik.Sitefinity.Web.PageRouteHandler.SetPageCacheDependencies() +476
Telerik.Sitefinity.Web.PageRouteHandler.handler_Unload(Object sender, EventArgs e) +35
System.Web.UI.Control.OnUnload(EventArgs e) +8885382
System.Web.UI.Control.UnloadRecursive(Boolean dispose) +241
System.Web.UI.Page.UnloadRecursive(Boolean dispose) +23
System.Web.UI.Page.ProcessRequestCleanup() +43
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +253
System.Web.UI.Page.ProcessRequest() +78
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.sfpageservice_a8f63540_de1e_4cb4_a407_639ae23f329d_6_basic_en_aspx.ProcessRequest(HttpContext context)
in
c:\Users\myusername\AppData\Local\Temp\Temporary ASP.NET Files\root\0d1a8e78\d1015611\App_Web_a8f63540-de1e-4cb4-a407-639ae23f329d_6_basic_en.aspx.d63928b6.hgexzo4u.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Exactly the same problem. Does anybody know a fix for this?
Hi Kevin and David,
Does this exception occur after executing some line of code from your custom route handler or does it happen before you enter the GetRouteData() method?
Regards,(((System.Web.HttpApplication)(sender)).Context.Error).StackTrace
at Telerik.Sitefinity.Web.PageRouteHandler.SetPageCacheDependencies()
at Telerik.Sitefinity.Web.PageRouteHandler.handler_Unload(Object sender, EventArgs e)
at System.Web.UI.Control.OnUnload(EventArgs e)
at System.Web.UI.Control.UnloadRecursive(Boolean dispose)
at System.Web.UI.Page.UnloadRecursive(Boolean dispose)
at System.Web.UI.Page.ProcessRequestCleanup()
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.sfpageservice_2128fa5d_03b3_416b_8bea_21d8666bdd98_2_vinites_aspx.ProcessRequest(HttpContext context) in c:\Users\userName\AppData\Local\Temp\Temporary ASP.NET Files\root\94d62d5f\300c1492\App_Web_2128fa5d-03b3-416b-8bea-21d8666bdd98_2_project.aspx.d63928b6.hdu7p3wo.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Hi Kevin,
Could you please clarify a bit - what do you mean by "a thread" ? Do you create a new thread using the System.Threading namespace? I also couldn't understand the following question - "Is it possible that the method uses an instance of Page" ? Could you try changing the logic to not use the Fluent API - App.WorkWith().etc but use the PageManager instead -
var manager = PageManager.GetManager();
var pages = manager.GetPageNodes().
etc.
The exception occurs after the GetRouteData() method. I checked one page that uses several user controls, and those controls appear to execute fully and correctly. The exception does not appear to be thrown from any custom code. It appears to be from within the Sitefinity engine.
For instance, when I'm debugging the site, the exception is not shown from any specific line of code. In fact the "Source Error" section of the error page states "No relevant source lines".
Hi David,
I tried your code and registered it in Global.asax.cs with
RouteTable.Routes.Insert(1, new CustomSitefinityRoute());
Now I always enter in the GetRouteData method, even if I do not access a URL like ~/features - but this is normal. I don't get such exception. I'm using a new 4.4 project. Could you try if your code works on a non upgraded 4.4 project? Also, how do you register the custom handler in the RouteTable?
Hi Lubomir,
public
class
CustomSitefinityRoute : SitefinityRoute
public
override
System.Web.Routing.RouteData GetRouteData(HttpContextBase httpContext)
//get the path from the httpContext variable and parse it
var virtuallPath =
this
.GetVirtualPathInternal(httpContext);
if
(virtuallPath.Contains(
"features"
))
//parse the acutal path to find the PageSiteNode from the sitemap provider
var sitemapProvider =
this
.GetSiteMapProvider();
if
(sitemapProvider ==
null
)
return
null
;
bool
isAdditional;
string
[] pars;
var node = sitemapProvider.FindSiteMapNode(
"/prices"
,
false
,
out
isAdditional,
out
pars);
if
(node !=
null
)
return
this
.GetRouteDataInternal(pars, httpContext.Request.QueryString, node);
return
base
.GetRouteData(httpContext);
public
static
void
RegisterType()
ObjectFactory.Container.RegisterType<SitefinityRoute, CustomSitefinityRoute>();
protected
void
Application_Start(
object
sender, EventArgs e)
Telerik.Sitefinity.Services.SystemManager.ApplicationStart +=
new
EventHandler<EventArgs>(SystemManager_ApplicationStart);
void
SystemManager_ApplicationStart(
object
sender, EventArgs e)
CustomSitefinityRoute.RegisterType();
RouteTable.Routes.Insert(1,
new
CustomSitefinityRoute());
Hi David,
I'm getting a little confused. I followed your instructions exactly but didn't get any exception. Also if I may suggest something - in Global.asax use the following code:
Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
and then
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
CustomSitefinityRoute.RegisterType();
if
(e.CommandName ==
"RegisterRoutes"
)
RouteTable.Routes.Insert(1,
new
CustomSitefinityRoute());
I implemented those changes in the Global.asax, and I'm still getting the error.
What are my options? I can zip up my project and database. Or is there some way to figure out what's happening in the Telerik.Sitefinity.Web.PageRouteHandler.SetPageCacheDependencies method?
Hi David,
I think it would be best if you zipped your project and database and sent it to us. That way we will be able to reproduce the problem locally and by debugging with the source code we'll be able to see why is this exception happening. If you wish you could open a separate support ticket and send the files or download details there.
Regards,I have submitted a support ticker (500482) with the project and database attached.
Hello David,
Okay, I suggest from now on to continue our communication on the matter in the support ticket.
Greetings,Was this ever resolved? I think it would be nice to post the solution for others :)
Hello Daniel,
I believe we resolved one specific issue. This is the code that you need to add in your custom route handler -
protected
override
SiteMapBase GetSiteMapProvider()
var siteMapProvider =
base
.GetSiteMapProvider();
SystemManager.CurrentHttpContext.Items[SiteMapBase.ProviderKey] = siteMapProvider;
return
siteMapProvider;