Copied to clipboard

Flag this post as spam?

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


  • vijaya shivgand 23 posts 122 karma points
    Apr 20, 2018 @ 17:19
    vijaya shivgand
    0

    Need advise on getting content

    Currently I have my web project and in same solution wrote controller which is using Umbracohelper and Typecontent to get content .

    But I want to isolate above as Umbraco Web just for managing the the content and another webapi project to get content.

    Is there any way I can call UmbracoHelper.TypedContent from my webapi project Please advise me.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Apr 20, 2018 @ 19:54
    Anders Bjerner
    0

    Hi vijaya,

    You might not always need an UmbracoHelper. The TypedContent method is a shortcut (helper method if you will) for getting the content item from Umbraco's content cache

    When you have a Web API controller, you can get a content item via UmbracoContext instead - eg. something like:

    using System.Net;
    using System.Net.Http;
    using Umbraco.Core.Models;
    using Umbraco.Web.WebApi;
    
    namespace YourNamespace
    {
    
        public class OurController : UmbracoApiController
        {
    
            public object GetContent(int contentId)
            {
    
                // Get the content item from the content cache
                IPublishedContent content = UmbracoContext.ContentCache.GetById(contentId);
    
                // Return a 404 error if the content item wasn't found
                if (content == null) return Request.CreateResponse(HttpStatusCode.NotFound);
    
                return new
                {
                    id = content.Id,
                    name = content.Name
                };
    
            }
    
        }
    
    }
    

    Hope that helps ;)

  • vijaya shivgand 23 posts 122 karma points
    Apr 20, 2018 @ 21:48
    vijaya shivgand
    0

    Thanks for reply Anders . I separated the controller in other project and installed UmbracoCms.Core package and I am thinking how to create instance of Umbraco context.

    I am getting exception at Line14

    protected void Application_Start() Line 13: { Line 14: GlobalConfiguration.Configure(WebApiConfig.Register); Line 15: }

    The exception is as follow

    [NullReferenceException: Object reference not set to an instance of an object.] Umbraco.Web.WebApi.UnhandledExceptionLogger..ctor() +5 Umbraco.Web.WebApi.UnhandedExceptionLoggerConfigurationAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) +49 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +154 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +54 System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) +105 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache() +390 System.Lazy1.CreateValue() +411 System.Lazy1.LazyInitValue() +153 System.Lazy1.get_Value() +75 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping() +14 System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) +48 System.Web.Http.Routing.<>c__DisplayClass4.<MapAttributeRoutes>b__1() +57 System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func1 initializer) +52 System.Web.Http.Routing.<>cDisplayClass2.0(HttpConfiguration config) +91 System.Web.Http.HttpConfiguration.EnsureInitialized() +23 System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +43 Services.WebApiApplication.Application_Start() in C:\UmbracoPOCStuff\UmbracoPoc2\Services\Global.asax.cs:14

    [HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +10063381 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

    [HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10044672 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95

Please Sign in or register to post replies

Write your reply to:

Draft