How to get UmbrcoContext in application start handlers and wired up events
In a class wired up to handle the IApplicationStart handler how do you get an UmbracoContext from either the applicationcontext or the umbracoapplicationbase?
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
You cannot call the UmbracoContext.Current from here as it is null and the constructors for the UmbracoContext are internal.
I need to get a PublishedContent node at this point to get its Url for a redirect.
I want to get an Umbracohelper but need the UmbracoContext to do so.
I am currently using Node as a workaround but this is the old way of doing things.... Is there a better way?
Any ideas how i can get this umbraco context or a published page from the cache at this point in the lifecycle?
I am actually using this in the PublishedContentRequest.Prepared event wired up in the application started event to be precise.
You should be OK with that Damian I think. You may not be able to access it directly in the ApplicationStarted event, but you can in the PublishedContentRequest.Prepared event when that is fired from the arguments, e.g.
Here is my code as it stands - it just feels a bit dirty using the old Node instead of Umbraco.GetTypedContent(id) for example. What i needed is a way to create an umbrco helper in the event - but UmbracoContext.Current is unavailable.
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
}
private void PublishedContentRequest_Prepared(object sender, EventArgs e)
{
// Check to make sure the request is valid
var request = sender as PublishedContentRequest;
if (request == null || !request.HasPublishedContent)
return;
//See if member is in lapsed role
//
var lapsed = Roles.IsUserInRole("Lapsed");
if (!lapsed) return;
var pageIsProtected = Access.IsProtected(request.PublishedContent.Id, request.PublishedContent.Path);
// Bounce to banned page
//
if (pageIsProtected)
{
const int lapsedId = 4497;
Node lapsedNode = null;
lapsedNode = new Node(lapsedId);
var lapsedUrl = "/account/lapsed";
try
{
lapsedUrl = lapsedNode.Url;
}
catch (Exception)
{
}
request.SetRedirect(lapsedUrl, 302);
}
}
Had a hunch i was wrong and after trying again i was right...
You CAN use UmbracoContext.Current within the Prepare event! I must have been trying to use it in the Startup handler previously!
So ive got rid of the node and moved to the proper way! now! :)
private void PublishedContentRequest_Prepared(object sender, EventArgs e)
{
// Check to make sure the request is valid
var request = sender as PublishedContentRequest;
if (request == null || !request.HasPublishedContent)
return;
//See if member is in lapsed role
//
var lapsed = Roles.IsUserInRole("Lapsed");
if (!lapsed) return;
var pageIsProtected = Access.IsProtected(request.PublishedContent.Id, request.PublishedContent.Path);
// Bounce to banned page
//
if (pageIsProtected)
{
const int lapsedId = 4497;
var lapsedUrl = "/account/lapsed";
try
{
var helper = new UmbracoHelper(UmbracoContext.Current);
var lapsedNode = helper.TypedContent(lapsedId);
if (lapsedNode != null)
lapsedUrl = lapsedNode.Url;
}
catch (Exception)
{
}
request.SetRedirect(lapsedUrl, 302);
}
}
I am trying to do someting like Articulate Git at line 49. In my case i wanna construct my url by concatenating to specifc nodes. for instance i wanna check all nodes of type X, then loop and create a route for each of them....
How to get UmbrcoContext in application start handlers and wired up events
In a class wired up to handle the IApplicationStart handler how do you get an UmbracoContext from either the applicationcontext or the umbracoapplicationbase?
You cannot call the UmbracoContext.Current from here as it is null and the constructors for the UmbracoContext are internal.
I need to get a PublishedContent node at this point to get its Url for a redirect.
I want to get an Umbracohelper but need the UmbracoContext to do so.
I am currently using Node as a workaround but this is the old way of doing things.... Is there a better way?
Any ideas how i can get this umbraco context or a published page from the cache at this point in the lifecycle?
I am actually using this in the PublishedContentRequest.Prepared event wired up in the application started event to be precise.
Thanks Damian
You should be OK with that Damian I think. You may not be able to access it directly in the ApplicationStarted event, but you can in the PublishedContentRequest.Prepared event when that is fired from the arguments, e.g.
Andy
Thats not quite what i was after...
Here is my code as it stands - it just feels a bit dirty using the old Node instead of Umbraco.GetTypedContent(id) for example. What i needed is a way to create an umbrco helper in the event - but UmbracoContext.Current is unavailable.
I see. Well another option might be to query from the published content - something like:
(that query is off the top of my head so almost certainly wrong... but illustrates the point).
Andy
Thought about that but its a generic type im looking for as a specific page so i guess i could search on name but too flaky.
Ill leave it using Node for now but don't know why UmbracoContext.Current is null when the PublishedContentRequest.Prepared event fires! ??
Maybe ill ask Stephan and im sure he can shed some light on it.
Cheers Andy.
Damian
Good idea! Do update back if you get a solution, I'd be interested to hear.
Ok - im a plonker....
Had a hunch i was wrong and after trying again i was right...
You CAN use UmbracoContext.Current within the Prepare event! I must have been trying to use it in the Startup handler previously!
So ive got rid of the node and moved to the proper way! now! :)
But PublishedContentRequest_Prepared runs for every request? Was that the intention?
I have been looking for a way to access the Umbraco context in the startup events, but with no luck so far.
/ Dennis
I am trying to do someting like Articulate Git at line 49. In my case i wanna construct my url by concatenating to specifc nodes. for instance i wanna check all nodes of type X, then loop and create a route for each of them....
someone have an idea how to achieve this ?
is working on a reply...