Problem with GlobalConfiguration.Configure(WebApiConfig.Register) and umbraco

0

Hi I have one Umbraco project and 1 web api project using Umbraco. I have installed on the second one UmbracoCms.Core and I have a controller that is UmbracoAppiController

 public class TestController : UmbracoApiController
    {

        [HttpGet]
        public IEnumerable<string> GetAll()
        {
            return new string[] { "value1", "value2" };
        }
}

I have global.asax.cs:

public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);            
        }
    }

and WebApiConfig.cs:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

When I try to make the web api call I'm getting:

Server Error in '/' Application.

Object reference not set to an instance of an object.  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 17:             // Code that runs on application startup Line 18: AreaRegistration.RegisterAllAreas(); Line 19:             GlobalConfiguration.Configure(WebApiConfig.Register); Line 20:         RouteConfig.RegisterRoutes(RouteTable.Routes);             Line 21:    }

Source File: C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs    Line: 19 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]    Umbraco.Web.WebApi.UnhandledExceptionLogger..ctor() +12    Umbraco.Web.WebApi.UnhandedExceptionLoggerConfigurationAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) +66 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +188    System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +62    System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) +130    System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()
+568    System.Lazy`1.CreateValue() +708    System.Lazy`1.LazyInitValue() +184    System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
+18    System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) +76    System.Web.Http.Routing.<>c__DisplayClass1_1.<MapAttributeRoutes>b__1()
+75    System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer) +66    System.Web.Http.Routing.<>c__DisplayClass1_0.<MapAttributeRoutes>b__0(HttpConfiguration config) +125    ShopApi1.Global.Application_Start(Object sender, EventArgs e) in C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs:19

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

[HttpException (0x80004005): Object reference not set to an instance of an object.]    System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +523    System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
+107    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +688


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3160.0

Please help

c#
umbraco
web.api
asked on Stack Overflow Oct 29, 2018 by Sonja

1 Answer

0

You don't need to do the Global.asax.cs or the WebApiConfig.cs files at all - the routing is automatically handled by Umbraco if you're inheriting from UmbracoApiController or SurfaceController.

Read the Routing documentation on our.umbraco.com, in particular the section about WebApi:

https://our.umbraco.com/documentation/Reference/Routing/WebApi

Based on this, your route for your TestController will be something like this:

~/Umbraco/Api/Test/GetAll
answered on Stack Overflow Oct 29, 2018 by Robert Foster

User contributions licensed under CC BY-SA 3.0