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
};
}
}
}
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
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.
Hi vijaya,
You might not always need an
UmbracoHelper
. TheTypedContent
method is a shortcut (helper method if you will) for getting the content item from Umbraco's content cacheWhen you have a Web API controller, you can get a content item via
UmbracoContext
instead - eg. something like:Hope that helps ;)
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.Lazy
1.CreateValue() +411 System.Lazy
1.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(Func
1 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
is working on a reply...