Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Calle Bjernekull 16 posts 38 karma points
    Mar 11, 2014 @ 09:11
    Calle Bjernekull
    1

    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:

     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.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 11, 2014 @ 09:34
    Ismail Mayat
    0

    Calle,

    You still need a context however you can get one from ApplicationContext.Current

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 11, 2014 @ 09:40
    Ismail Mayat
    0

    Calle,

    Just found this code in my mvc model

    var helper = new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId))
    

    So you could try along those lines.

    Regards

    Ismail

  • Calle Bjernekull 16 posts 38 karma points
    Mar 11, 2014 @ 09:42
    Calle Bjernekull
    0

    Actually, thats where I've been looking. But how do I get an UmbracoContext from an Umbraco.Core.ApplicationContext? Any example?

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 11, 2014 @ 10:12
    Ismail Mayat
    0

    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

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 11, 2014 @ 10:39
    David Brendel
    0

    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

  • Calle Bjernekull 16 posts 38 karma points
    Mar 11, 2014 @ 16:31
    Calle Bjernekull
    0

    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:

    Umbraco.Web.Models.RenderModel..ctor(IPublishedContent content)

    I've reflected the code, and it seems like UmbracoContext.Current.PublishedContentRequest is null.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 11, 2014 @ 16:41
    Ismail Mayat
    0

    Calle,

    Your missing overloaded contructor so in mine i have

            public SearchFormModel()
            : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId))
        {
    
            Init();
    
        }
    
        public SearchFormModel(IPublishedContent content)
            : base(content)
        {
            Init();
        }
    

    ignore the init bit thats just my private function doing some stuff

    Regards

    Ismail

Please Sign in or register to post replies

Write your reply to:

Draft