I have a couple of controllers that is supposed to deliver JSON-data instead of HTML. So I created a JSON-controller to do this. This is a normal MVC-controller that works just fine. Looks like this:
public class JsonController : Controller { [HttpGet] public ActionResult MyData() { var myData = GetSomeData(); //Would like to use Umbraco.TypedMedia() and Umbraco.TypedContent() here JavaScriptSerializer serializer = new JavaScriptSerializer(); return (ActionResult)this.Content(serializer.Serialize(myData), "application/json"); }
But I would like to get some IPublishedContent in here, which ususlly can be done by UmbracoHelper. How can I create a UmbracoHelper without a UmbracoContext here? Or can I get an UmbracoContext in this controller? UmbracoContext.Current is null, so that won't work.
When i have created controllers they have always inherited from surfacecontroller so i have had umbracocontext. Just found this maybe this will help? http://issues.umbraco.org/issue/U4-1717 some code there to get umbracocontext
Yes, the controller is inside my application. But doesn't have a view that inherits Umbraco.Web.Mvc.UmbracoViewPage (acually, it doesnt hav any view at all, because it's rendering json)
However, I've been trying the SurfaceController, but experience some problems with it. Here's one example:
I'm trying to create a model that inherits RenderModel. This model is also used in other views, so that's why it's inherits RenderModel. But when calling the constructor I get an NullReferenceException in:
Get an UmbracoHelper without UmbracoContext
I have a couple of controllers that is supposed to deliver JSON-data instead of HTML. So I created a JSON-controller to do this. This is a normal MVC-controller that works just fine. Looks like this:
But I would like to get some IPublishedContent in here, which ususlly can be done by UmbracoHelper. How can I create a UmbracoHelper without a UmbracoContext here? Or can I get an UmbracoContext in this controller? UmbracoContext.Current is null, so that won't work.
Calle,
You still need a context however you can get one from ApplicationContext.Current
Regards
Ismail
Calle,
Just found this code in my mvc model
So you could try along those lines.
Regards
Ismail
Actually, thats where I've been looking. But how do I get an UmbracoContext from an Umbraco.Core.ApplicationContext? Any example?
Calle,
When i have created controllers they have always inherited from surfacecontroller so i have had umbracocontext. Just found this maybe this will help? http://issues.umbraco.org/issue/U4-1717 some code there to get umbracocontext
hi Calle,
do your controllers are inside your umbracp application? Then i would suggest to inherit from the umbraco surfacecontrollers.
With this you have access to the helper.
David
Yes, the controller is inside my application. But doesn't have a view that inherits Umbraco.Web.Mvc.UmbracoViewPage (acually, it doesnt hav any view at all, because it's rendering json)
However, I've been trying the SurfaceController, but experience some problems with it. Here's one example:
I'm trying to create a model that inherits RenderModel. This model is also used in other views, so that's why it's inherits RenderModel. But when calling the constructor I get an NullReferenceException in:
I've reflected the code, and it seems like UmbracoContext.Current.PublishedContentRequest is null.
Calle,
Your missing overloaded contructor so in mine i have
ignore the init bit thats just my private function doing some stuff
Regards
Ismail
is working on a reply...