TypeInitializationException throw when calling Node.GetCurrent() in external Library
Hi,
We're building an external library that queries a search indexing system we use. The library is designed to be dropped into any web project, including umbraco sites.
Now, when it's being used in an Umbraco site, we want to take advantage of some Umbraco features by getting the current page node via Node.GetCurrent(). If Node.GetCurrent() is called in a standard non-Umbraco ASP.NET website project, then GetCurrent() will throw a NullReferenceException which can catch in a try/catch block, so no problem. However, somewhere somehow, a TypeInitializationException is being thrown when a call to Node.GetCurrent() is made, and it's not being caught by our try/catch block.
[code]try
{
Node currentPage = Node.GetCurrent();
// Do something with current page
}
catch
{
// Not an Umbraco website, so do something else
}[/code]
So I guess the question is, is there some programmic way we get detect whether or not we're executing inside an Umbraco instance, or an ordinary ASP.NET website?
My work colleague had the idea of detecting the presense of the /data/umbraco.config file, does this seem a good idea, to you?
You could try and test for querystring umbPage if that is not present then you are not in umbraco site? in umbraco when u view page the httphandler gives you mysite/mypage.aspx but under the hood the actual request is default.aspx?umbPage=1234 etc
TypeInitializationException throw when calling Node.GetCurrent() in external Library
Hi,
We're building an external library that queries a search indexing system we use. The library is designed to be dropped into any web project, including umbraco sites.
Now, when it's being used in an Umbraco site, we want to take advantage of some Umbraco features by getting the current page node via Node.GetCurrent(). If Node.GetCurrent() is called in a standard non-Umbraco ASP.NET website project, then GetCurrent() will throw a NullReferenceException which can catch in a try/catch block, so no problem. However, somewhere somehow, a TypeInitializationException is being thrown when a call to Node.GetCurrent() is made, and it's not being caught by our try/catch block.
[code]try
{
Node currentPage = Node.GetCurrent();
// Do something with current page
}
catch
{
// Not an Umbraco website, so do something else
}[/code]
So I guess the question is, is there some programmic way we get detect whether or not we're executing inside an Umbraco instance, or an ordinary ASP.NET website?
My work colleague had the idea of detecting the presense of the /data/umbraco.config file, does this seem a good idea, to you?
Dominic,
You could try and test for querystring umbPage if that is not present then you are not in umbraco site? in umbraco when u view page the httphandler gives you mysite/mypage.aspx but under the hood the actual request is default.aspx?umbPage=1234 etc
Regards
Ismail
Hi Dominic,
You could try this,
try
{
if (File.Exists(HttpContext.Current.Server.MapPath("/data/umbraco.config")))
{
XmlNode node = ((IHasXmlNode)library.GetXmlNodeById(Node.GetCurrent().Id.ToString()).Current).GetNode();
}
}
catch
{
}
Thanks,
Osama
is working on a reply...